-->
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.  [ 5 posts ] 
Author Message
 Post subject: One-to-many mappings problem
PostPosted: Sun Nov 23, 2003 5:26 am 
Newbie

Joined: Sun Nov 23, 2003 4:46 am
Posts: 6
Hi guys,

I'm new to Hibernate and I'm trying out a one-to-many design mapping, but I'm stuck in between. Can someone please help me?

Storyline,
Table 1 consists of 3 column where visit_id is the primary key
Table 2 consist of 3 column where crane_id and visit_id is the composite primary key, visit id is the foreign key of table 1.
In some how, I need to manually assign the visit id, and crane id.
1 trx_visit can have many trx_visit_crane
I trx_visit_crane can only belongs to 1 trx_visit.

Here is the case, 2 tables
- table 1, straight forward
TrxVisit consists of visit_id, visit_type, and visit_carrier

<class name="TrxVisit" table="trx_visit">
<id name="visitId" column="visit_id" type="java.lang.String">
<generator class="assigned"/>
</id>
<property name="visitType" column="visit_type" type="java.lang.String" length="2" not-null="true"/>
<property name="visitCarrier" column="visit_carrier" type="java.lang.String" length="255" not-null="true"/>
<set name="visitId" table="trx_visit_crane">
<key>
<column name="visitId" not-null="true"/>
</key>
<one-to-many class="berth.hibernate.entity.TrxVisitCrane"/>
</set>
</class>

-table 2
<class name="TrxVisitCrane" table="trx_visit_crane">
<composite-id>
<key-property name="craneId" column="crane_id" type="java.lang.String" length="5"/>
<key-property name="visitId" column="visit_id" type="java.lang.String" length="12"/>
</composite-id>
<property name="craneMph" column="crane_mph" type="java.lang.Integer" length="3" not-null="true"/>
<many-to-one name="visitId" column="visit_id" not-null="true" class="berth.hibernate.entity.TrxVisit" insert="false" update="false"/>
</class>

Did I define the mapping correctly?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 23, 2003 12:18 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
<key-many-to-one> tag is what you want.
Section 4.1.5 of the reference guide.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Thanks for your input
PostPosted: Sun Nov 23, 2003 10:10 pm 
Newbie

Joined: Sun Nov 23, 2003 4:46 am
Posts: 6
Hi,

Good advice, but where I trying to do something like below, it gives me error : No persisit for: java.lang.String error executing query for TrxVisit

<class name="berth.hibernate.entity.TrxCrane" table="trx_crane">
<composite-id>
<key-property name="craneId" column="crane_id" type="java.lang.String" length="5"/>
<key-many-to-one name="cvisitId" column="cvisit_id" type="java.lang.String" length="10"/>
</composite-id>
<property name="craneMph" column="crane_mph" type="java.lang.Integer" length="3" not-null="true"/>
</class>

<class name="berth.hibernate.entity.TrxVisit" table="trx_visit">
<id name="visitId" column="visit_id" type="java.lang.String">
<generator class="assigned"/>
</id>
<property name="visitType" column="visit_type" type="java.lang.String" length="2" not-null="true"/>
<property name="visitCarrier" column="visit_carrier" type="java.lang.String" length="255" not-null="true"/>
<set name="cranes">
<key column="cvisit_id"/>
<one-to-many class="berth.hibernate.entity.TrxCrane"/>
</set>
</class>

below are my class structure
public class TrxVisit implements Serializable {
private String visitId;
private String visitType;
private String visitCarrier;
private Set cranes;

/** full constructor */
/** default constructor */
/** all the getter and setter method */

public boolean equals(Object other) {
if ( !(other instanceof TrxVisit) ) return false;
TrxVisit castOther = (TrxVisit) other;
return new EqualsBuilder()
.append(this.getVisitId(), castOther.getVisitId())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getVisitId())
.toHashCode();
}
}

public class TrxCrane implements Serializable {
private String craneId;
private String cvisitId;
private Integer craneMph;
/** full constructor */
/** default constructor */
/** all getter and setter method */
public boolean equals(Object other) {
if ( !(other instanceof TrxCrane) ) return false;
TrxCrane castOther = (TrxCrane) other;
return new EqualsBuilder()
.append(this.getCraneId(), castOther.getCraneId())
.append(this.getCvisitId(), castOther.getCvisitId())
.isEquals();
}

public int hashCode() {
return new HashCodeBuilder()
.append(getCraneId())
.append(getCvisitId())
.toHashCode();
}
}


Top
 Profile  
 
 Post subject: Re: Thanks for your input
PostPosted: Mon Nov 24, 2003 5:32 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
buttercup wrote:
<key-many-to-one name="cvisitId" column="cvisit_id" type="java.lang.String" length="10"/>


Sure, you map the many-to-one on a String.

Code:
<key-many-to-one name="cvisit" column="cvisit_id" class="berth.hibernate.entity.TrxVisit" length="10"/>


There may be some samples on the forum you can search.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Ok.. Thanks
PostPosted: Tue Nov 25, 2003 3:11 am 
Newbie

Joined: Sun Nov 23, 2003 4:46 am
Posts: 6
Let me try :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.