aspose file tools*
The moose likes Object Relational Mapping and the fly likes HIbernate Criteria when fired on VIEW returs list with NULL values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "HIbernate Criteria when fired on VIEW returs list with NULL values" Watch "HIbernate Criteria when fired on VIEW returs list with NULL values" New topic
Author

HIbernate Criteria when fired on VIEW returs list with NULL values

Kush Saraiya
Greenhorn

Joined: Mar 21, 2011
Posts: 4
Hi,

I am using criteria query to fetch list of records from database VIEW.[code=java] I have created the corresponding HBM.xml and POJO files.

OuterView.hbm.xml corresponds to OuterView.java, which contains a composite key of class InnerView.java.
OuterView.java contains only one attribute as InnerView and InnerView contains all the column names and their getters and setters.

I am creating criteria on OuterView.java and the query is being fired too. But when I am fetching the returned list, it gives me a list with NULL objects in it. I cannot get the data neither in OuterView nor in InnerView.

Can you please help me out? Is there something that I am missing?


Code for reference:

HBM.xml:

<hibernate-mapping>
<class name="OuterView" table="vw_user_basic_info" catalog="ideas">
<comment></comment>
<composite-id name="id" class="InnerView">
<key-property name="accUserId" type="long">
<column name="acc_user_id" />
</key-property>
<key-property name="accountId" type="long">
<column name="account_id" />
</key-property>
<key-property name="accUserType" type="long">
<column name="acc_user_type" />
</key-property>
<key-property name="accOwnerFlg" type="java.lang.Integer">
<column name="acc_owner_flg" />
</key-property>
<key-property name="firstName" type="string">
<column name="first_name" length="50" />
</key-property>
<key-property name="middleName" type="string">
<column name="middle_name" length="50" />
</key-property>
<key-property name="lastName" type="string">
<column name="last_name" length="50" />
</key-property>
<key-property name="accountName" type="string">
<column name="account_name" length="100" />
</key-property>
<key-property name="userName" type="string">
<column name="user_name" length="50" />
</key-property>
<key-property name="userPwd" type="string">
<column name="user_pwd" length="500" />
</key-property>
<key-property name="securityQuestion" type="string">
<column name="security_question" length="100" />
</key-property>
<key-property name="securityAnswer" type="string">
<column name="security_answer" length="100" />
</key-property>
<key-property name="forgetFlag" type="int">
<column name="forget_flag" />
</key-property>
<key-property name="salt" type="string">
<column name="salt" length="200" />
</key-property>
</composite-id>
</class>
</hibernate-mapping>


OuterView.java:

public class OuterView implements java.io.Serializable
{

// Fields

private InnerView id;

// Constructors

/** default constructor */
public OuterView()
{
}

/** full constructor */
public OuterView(InnerView id)
{
this.id = id;
}

// Property accessors
public InnerView getId()
{
return this.id;
}

public void setId(InnerView id)
{
this.id = id;
}

}


InnerView.java:

public class InnerView implements java.io.Serializable
{

// Fields

private long accUserId;
private long accountId;
private long accUserType;
private Integer accOwnerFlg;
private String firstName;
private String middleName;
private String lastName;
private String accountName;
private String userName;
private String userPwd;
private String securityQuestion;
private String securityAnswer;
private int forgetFlag;
private String salt;

// Constructors

/** default constructor */
public InnerView()
{
}

/** minimal constructor */
public InnerView(long accUserId, long accountId, long accUserType,
String firstName, String accountName, String userName,
String userPwd, String securityQuestion, String securityAnswer,
int forgetFlag, String salt)
{
this.accUserId = accUserId;
this.accountId = accountId;
this.accUserType = accUserType;
this.firstName = firstName;
this.accountName = accountName;
this.userName = userName;
this.userPwd = userPwd;
this.securityQuestion = securityQuestion;
this.securityAnswer = securityAnswer;
this.forgetFlag = forgetFlag;
this.salt = salt;
}

/** full constructor */
public InnerView(long accUserId, long accountId, long accUserType,
Integer accOwnerFlg, String firstName, String middleName,
String lastName, String accountName, String userName,
String userPwd, String securityQuestion, String securityAnswer,
int forgetFlag, String salt)
{
this.accUserId = accUserId;
this.accountId = accountId;
this.accUserType = accUserType;
this.accOwnerFlg = accOwnerFlg;
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.accountName = accountName;
this.userName = userName;
this.userPwd = userPwd;
this.securityQuestion = securityQuestion;
this.securityAnswer = securityAnswer;
this.forgetFlag = forgetFlag;
this.salt = salt;
}

// Property accessors
public long getAccUserId()
{
return this.accUserId;
}

public void setAccUserId(long accUserId)
{
this.accUserId = accUserId;
}

public long getAccountId()
{
return this.accountId;
}

public void setAccountId(long accountId)
{
this.accountId = accountId;
}

public long getAccUserType()
{
return this.accUserType;
}

public void setAccUserType(long accUserType)
{
this.accUserType = accUserType;
}

public Integer getAccOwnerFlg()
{
return this.accOwnerFlg;
}

public void setAccOwnerFlg(Integer accOwnerFlg)
{
this.accOwnerFlg = accOwnerFlg;
}

public String getFirstName()
{
return this.firstName;
}

public void setFirstName(String firstName)
{
this.firstName = firstName;
}

public String getMiddleName()
{
return this.middleName;
}

public void setMiddleName(String middleName)
{
this.middleName = middleName;
}

public String getLastName()
{
return this.lastName;
}

public void setLastName(String lastName)
{
this.lastName = lastName;
}

public String getAccountName()
{
return this.accountName;
}

public void setAccountName(String accountName)
{
this.accountName = accountName;
}

public String getUserName()
{
return this.userName;
}

public void setUserName(String userName)
{
this.userName = userName;
}

public String getUserPwd()
{
return this.userPwd;
}

public void setUserPwd(String userPwd)
{
this.userPwd = userPwd;
}

public String getSecurityQuestion()
{
return this.securityQuestion;
}

public void setSecurityQuestion(String securityQuestion)
{
this.securityQuestion = securityQuestion;
}

public String getSecurityAnswer()
{
return this.securityAnswer;
}

public void setSecurityAnswer(String securityAnswer)
{
this.securityAnswer = securityAnswer;
}

public int getForgetFlag()
{
return this.forgetFlag;
}

public void setForgetFlag(int forgetFlag)
{
this.forgetFlag = forgetFlag;
}

public String getSalt()
{
return this.salt;
}

public void setSalt(String salt)
{
this.salt = salt;
}
}


Hibernate Code:

Criteria criteria = session.createCriteria(VwUserBasicInfo.class);
List resultList = criteria.list();
Michael Freake
Greenhorn

Joined: Jun 03, 2009
Posts: 24
Hi,

Maybe try having Hibernate output the sql it is running on the database. If you then run this same query on the database using your tool and get different results, then it has to do with your configuration.

You can put the following in your hibernate.cfg.xml file to see nicely formatted SQL:

<property name="show_sql">true</property>
<property name="format_sql">true</property>
Kush Saraiya
Greenhorn

Joined: Mar 21, 2011
Posts: 4
Hi Michael.

I tried to see the query in console and it is being fired. Also the data is properly being fetched using my tool. The only thing is that hibernate is not binding the data values with those in java file. This is quite wierd because it is firing the query properly using the criteria but not putting the same obtained values in that java file on return.
Michael Freake
Greenhorn

Joined: Jun 03, 2009
Posts: 24
Are any of your queries working properly? I'm wondering if the problem can be isolated to just this particular query.

I imagine you are getting back an empty array of values currently, unless some error is being thrown.
Michael Freake
Greenhorn

Joined: Jun 03, 2009
Posts: 24
Also, shouldn't your class="..." include your package as name?
Kush Saraiya
Greenhorn

Joined: Mar 21, 2011
Posts: 4
Yeah the class has the included package. It's just I copy pasted the class only for reference.

Yes, the problem is with this query only. Other queries are working fine. Although other queries are fired on tables and not on VIEWS. Can the problem lie here?
Michael Freake
Greenhorn

Joined: Jun 03, 2009
Posts: 24
I am currently using Hibernate with views and have no issues at all, so I do not think that would be the issue...

Do you have a mapping file for both of your classes as well as reference to both classes in your hibernate.cfg.xml file?
Kush Saraiya
Greenhorn

Joined: Mar 21, 2011
Posts: 4
Hi, I do not have mapping files for both the classes. I just have hbm.xml for OuterView.class (as given in first post).

And why should the classes have reference in hibernate.cfg.xml? hibernate.cfg.xml just contains the config parameters as dialect, login etc.
Michael Freake
Greenhorn

Joined: Jun 03, 2009
Posts: 24
Sorry, I think you may have misunderstood. Each pojo you are mapping to the database should be listed in your hibernate.cfg.xml file like so:

Peter Jerald
Greenhorn

Joined: May 31, 2011
Posts: 3
Hi,


I too facing this same problem. All the required parameters for executing criteria query set properly but i am getting null values in one of the column of the list.


Please help me to solve this problem.


Regards,

A. Peter Jerald.
Peter Jerald
Greenhorn

Joined: May 31, 2011
Posts: 3
Hi,

I have fixed this issue. Now its working perfectly.


The issue is with HBM POJO Class.. I have fixed this issue by this way.


In my HBM POJO Class, one of the column called status is returned as null, When i lookout the class i can able to see in setter method

private String status=null;

public void setStatus(String status) {
status = status;
}

As of I know the original value of Status=null is taken throughout the entire operation.So to solve this i placed this operator on setter method shown below,


public void setStatus(String status) {
this.status = status;
}

Now i am getting all the status column values from DB.


So, Kindly check with corresponding HBM POJO Class file.


Regards,

A. Peter Jerald


 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HIbernate Criteria when fired on VIEW returs list with NULL values