-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem when use Component as Composite key
PostPosted: Sun Jan 09, 2011 5:58 pm 
Newbie

Joined: Tue Aug 05, 2008 3:11 pm
Posts: 7
Could any one points out what I did wrong? Much appreciated.

java test codes:

Parent p = new Parent();
ParentId pidObj = new ParentId();
pidObj.setPid(3);
pidObj.setVal("a value");
p.setId(pidObj);

session.load(p, pidObj);

Child c = new Child();
ChildId cidObj = New ChildId();
cidObj.setCid(10);
cidObj.setVal("a value");
c.setId(cidObj);

c.setParent(p);
//p.getChildren().add(c);
session.save(c);
session.flush();

Exception thrown as following:

Hibernate:
insert
into
child
(child_id, value, name)
values
(?, ?, ?)
[main] ERROR JDBCExceptionReporter - Field 'parent_id' doesn't have a default value

As you could see, the problem is: when insert to child table, required field parent_id was not inclued in hibernated created SQL.
I am not sure why hibernate did not use the parent_id i loaded from database (in the above case, it's 3).

Does any one have any idea to fix this?
All the java classes, hibernate mappings and database tables are attached below.

Public class Child implements java.io.Serializable {
private ChildId id;
private String name;
private Paren parent;

// setter & getter methods below //
}

public class ChildId implements java.io.Serializable {
private Integer cid;
private String val;

// getter and setter methods //

public boolean equals(Object other) {
// implements equals() //
}

public int hashCode() {
// implements hashCode() //
}
}

Public class Parent implements java.io.Serializable {
private ParentId id;
private String name;
private Set children = new HashSet(0);

// setter & getter methods below //
}

public class ParentId implements java.io.Serializable {
private Integer pid;
private String val; // note this column similar to that in ChildId class, same value too //

// getter and setter methods //

public boolean equals(Object other) {
// implements equals() //
}

public int hashCode() {
// implements hashCode() //
}
}

<hibernate-mapping>
<class name="Child" table="child">
<composite-id name="id" class="ChildId">
<key-property name="cid" type="java.lang.Integer">
<column name="child_id" />
</key-property>
<key-property name="val" type="java.lang.String">
<column name="value" />
</key-property>
</composite-id>

<property name="name" type="java.lang.String">
<column name="child_name" not-null="true" />
</property>
<many-to-one name="parent" class="Parent" fetch="select" insert="false" update="false">
<column name="parent_id" not-null="true" />
<column name="value" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="Parent" table="parent" dynamic-update="true">
<composite-id name="id" class="ParentId">
<key-property name="pid" type="java.lang.Integer">
<column name="parent_id" />
</key-property>
<key-property name="val" type="java.lang.String">
<column name="value" />
</key-property>
</composite-id>

<property name="name" type="java.lang.String">
<column name="parent_name" not-null="true" />
</property>

<set name="children" inverse="true">
<key>
<column name="child_id" not-null="true" />
<column name="value" not-null="true" />
</key>
<one-to-many class="Child" />
</set>
</class>
</hibernate-mapping>

CREATE TABLE parent (
parent_id INTEGER NOT NULL,
value VARCHAR(6) NOT NULL,
parent_name VARCHAR(50) NOT NULL,
PRIMARY KEY(parent_id, value)
);

CREATE TABLE child (
child_id INTEGER NOT NULL,
value VARCHAR(6) NOT NULL,
child_name VARCHAR(50) NOT NULL,
parent_id INTEGER NOT NULL,
PRIMARY KEY(child_id, value),
FOREIGN KEY (parent_id, value) REFERENCES parent (parent_id, value)
);


Top
 Profile  
 
 Post subject: Re: Problem when use Component as Composite key
PostPosted: Mon Jan 10, 2011 5:57 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Code:
<many-to-one name="parent" class="Parent" fetch="select" insert="false" update="false">


You should remove the insert="false" in your <many-to-one> mapping.


Top
 Profile  
 
 Post subject: Re: Problem when use Component as Composite key
PostPosted: Fri Jan 21, 2011 12:12 am 
Newbie

Joined: Tue Aug 05, 2008 3:11 pm
Posts: 7
thanks for your response. Hibernate will throw exception if removing \the insert="false" in your <many-to-one> mapping.

No one has answer for this?

thanks


Top
 Profile  
 
 Post subject: Re: Problem when use Component as Composite key
PostPosted: Fri Jan 21, 2011 2:57 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
What exception?


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