-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Can i use VIEWS in Hibernate
PostPosted: Fri Mar 19, 2004 7:04 pm 
Beginner
Beginner

Joined: Mon Mar 08, 2004 5:22 pm
Posts: 35
I have some legacy [AS400] VIEW which i HAVE TO use to SELECT the data. There is no need to UPDATEs.
If i can use VIEWS how do i specify ID element in hibernate mapping ?
VIEWS do not have primary keys right ?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 20, 2004 5:56 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
it depends on your data...
if you can see a unique column in your view, use it.
If not use a composite id


Top
 Profile  
 
 Post subject: VIEW WITH COMPOSITE ID
PostPosted: Mon Mar 22, 2004 11:43 am 
Beginner
Beginner

Joined: Mon Mar 08, 2004 5:22 pm
Posts: 35
I have a view called "PRTDIRVIEW" and Person class with all
following fields with set/get methods. Here is my mapping.
I have to run the test by auction id[where auction id = 'AAA'].
This auction id has 50 records in the database.
In my result i get number of records 50, but same record
[1st record repeated 50 times]. I just need to select.
[EX: 1st person in data base is 'Smith', he is repeated 50 times
in result].



<class name="com.auctioninfo.model.Person" table="PRTDIRVIEW">
<composite-id>
<key-property name="PRTLNAME"/>
<key-property name="PRTFNAME"/>
</composite-id>
<property name="AUCTID">
<column name="AUCTID" />
</property>
<property name="FAX">
<column name="FAX" />
</property>
<property name="PHONE">
<column name="PHONE" />
</property>
<property name="PSEMLADDR">
<column name="PSEMLADDR" />
</property>
<property name="PSDEPTDESC">
<column name="PSDEPTDESC" />
</property>
<property name="PRTFLAG">
<column name="PRTFLAG" />
</property>
<property name="PRTEMPST">
<column name="PRTEMPST"/>
</property>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 22, 2004 12:41 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
what sql query is generated?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 22, 2004 12:59 pm 
Beginner
Beginner

Joined: Mon Mar 08, 2004 5:22 pm
Posts: 35
Sorry i read the manual once again. Realized i need to implement composite-id for the view. So i created the following stuff, since my VIEW does not have any pk/unique keys.
Can you tell me how to implement equals(), hashCode() for PersonPK

I get the following error
net.sf.hibernate.MappingException: Error reading resource: com/auctioninfo/model/Person.hbm.xml
at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:325)
at com.auctioninfo.dao.AuctionInfoDAO.prepareAuctionInfoConfiguration(AuctionInfoDAO.java:21)
at com.auctioninfo.dao.AuctionInfoDAO.findAuctionInfo(AuctionInfoDAO.java:31)
at com.test.HibernateTest.testAuctionInfo(HibernateTest.java:23)
at java.lang.reflect.Method.invoke(Native Method)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:329)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:218)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:151)
Caused by: net.sf.hibernate.MappingException: composite-id class must override equals() and hashCode(): com.auctioninfo.model.PersonPK
at net.sf.hibernate.cfg.Binder.bindRootClass(Binder.java:295)
at net.sf.hibernate.cfg.Binder.bindRoot(Binder.java:1204)
at net.sf.hibernate.cfg.Configuration.add(Configuration.java:247)
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:281)
at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:322)
... 15 more

PersonPK has set/get methods in Person class.


public class PersonPK implements Serializable
{
public PersonPK(){}

private String PRTFNAME;
private String PRTLNAME;

public String getPRTFNAME(){return PRTFNAME;}
public String getPRTLNAME(){return PRTLNAME;}
public void setPRTFNAME(String pRTFNAME){PRTFNAME = pRTFNAME;}
public void setPRTLNAME(String pRTLNAME){PRTLNAME = pRTLNAME;}
}

My mapping
<hibernate-mapping>
<class name="com.auctioninfo.model.Person" table="PRTDIRVIEW">
<composite-id name="personName" class="com.auctioninfo.model.PersonPK">
<key-property name="PRTLNAME"/>
<key-property name="PRTFNAME"/>
</composite-id>
<property name="AUCTID">
<column name="AUCTID" />
</property>
<property name="FAX">
<column name="FAX" />
</property>
<property name="PHONE">
<column name="PHONE" />
</property>
<property name="PSEMLADDR">
<column name="PSEMLADDR" />
</property>
<property name="PSDEPTDESC">
<column name="PSDEPTDESC" />
</property>
<property name="PRTFLAG">
<column name="PRTFLAG" />
</property>
<property name="PRTEMPST">
<column name="PRTEMPST"/>
</property>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 22, 2004 1:00 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
search on the site, there are many way to write these methods...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 22, 2004 2:47 pm 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
About making equals() and hashCode(). One good way is to use the builders in jakarta commons-lang. They produce results in accordance to the very good guidelines presented in the Effective Java book (search google and you'll find some sample chapters). Additionally, keeping in mind http://www.hibernate.org/109.html, i.e. prefer to use some static data instead of object id, a good equals and hashCode for some class Foo could be (assuming the class contains creationDate and creatorId properties which presumably don't change):

Code:
public boolean equals(Object o) {
    if (!(o instanceof Foo)) {
        return false;
    }
    Foo rhs = (Foo) o;
    return new EqualsBuilder()
                 .append(creationDate, rhs.creationDate)
                 .append(creatorId, rhs.creatorId)
                 .isEquals();
}

public int hashCode() {
    return new HashCodeBuilder(23, 43)
                 .append(creationDate)
                 .append(creatorId)
                 .toHashCode();
}   




Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 22, 2004 6:59 pm 
Beginner
Beginner

Joined: Mon Mar 08, 2004 5:22 pm
Posts: 35
Thank you JOIB. i got it worked.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.