Hibernate version: 3.1
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class entity-name="hra_address" table="hra_address" node="hra_address">
<composite-id name="emp_add_id">
<key-many-to-one name="emp_pk" entity-name="hra_personal_details"></key-many-to-one>
<key-property name="address_type" column="address_type" type="java.lang.String" length="50" ></key-property>
</composite-id>
<property name="house_no" column="house_no" node="house_no"
type="java.lang.String" length="30"/>
<property name="city" column="city" node="city"
type="java.lang.String" length="30"/>
<property name="state" column="state" node="state"
type="java.lang.String" length="30"/>
<property name="country" column="country" node="country"
type="java.lang.String" length="30"/>
<property name="postal_code" column="postal_code" node="postal_code"
type="java.lang.String" length="30"/>
</class>
<class entity-name="hra_personal_details" table="hra_personal_details" node="hra_personal_details">
<id name="emp_pk" column="emp_pk" node="emp_pk"
type="java.lang.String">
<generator class="uuid" />
</id>
<property name="emp_id" column="emp_id" node="emp_id"
type="java.lang.String" length="15" unique="true" not-null="true"/>
<property name="first_name" column="first_name" node="first_name"
type="java.lang.String" length="40" not-null="true"/>
<property name="middle_name" column="middle_name" node="middle_name"
type="java.lang.String" length="40"/>
<property name="last_name" column="last_name" node="last_name"
type="java.lang.String" length="40" not-null="true"/> </class>
</hibernate-mapping>
Full stack trace of any exception that occurs:
drop table if exists hra_address
drop table if exists hra_personal_details
create table hra_address (emp_pk varchar(255) not null, address_type varchar(30), house_no varchar(30), city varchar(30), state varchar(30), country varchar(30), postal_code varchar(30), primary key (emp_pk))
create table hra_personal_details (emp_pk varchar(255) not null, emp_id varchar(15) not null unique, first_name varchar(40) not null, middle_name varchar(40), last_name varchar(40) not null, primary key (emp_pk))
alter table hra_address add index FKBBB2504CFFE502BD (emp_pk), add constraint FKBBB2504CFFE502BD foreign key (emp_pk) references hra_personal_details (emp_pk)
Generated emp_id: 402881de0a9302b7010a9302cda80001
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): hra_address
at org.hibernate.id.Assigned.generate(Assigned.java:33)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:91)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:591)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:569)
at TestCompositeKey.createData(TestCompositeKey.java:79)
at TestCompositeKey.main(TestCompositeKey.java:129)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.tools.ant.taskdefs.ExecuteJava.run(ExecuteJava.java:202)
at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:134)
at org.apache.tools.ant.taskdefs.Java.run(Java.java:710)
at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:178)
at org.apache.tools.ant.taskdefs.Java.execute(Java.java:84)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
at org.apache.tools.ant.Task.perform(Task.java:364)
at org.apache.tools.ant.Target.execute(Target.java:341)
at org.apache.tools.ant.Target.performTasks(Target.java:369)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
at org.apache.tools.ant.Main.runBuild(Main.java:668)
at org.apache.tools.ant.Main.startAnt(Main.java:187)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)
Name and version of the database you are using: Mysql 5.0
Hello all,
I am not an expert of hibernate, so pardon me if this is not the type of question that I have asked to intelligent people like you.
I have defined a composite-id in mapping file.
I am using xml-mapping only to define my entity classes.
When I am persisting the data, I am getting this. Anyone knowing the reason, please reply asap.
Code for persistence
______________________________________________
public void createData(){
try{
Session s = _sessions.openSession();
Transaction t = s.beginTransaction();
Map emp = new HashMap();
emp.put("emp_id", "200201018");
emp.put("first_name", "Gagan");
emp.put("middle_name", "");
emp.put("last_name", "Jain");
Map address = new HashMap();
address.put("emp_pk", emp);
address.put("address_type", "2");
address.put("house_no", "H No:195");
address.put("city", "dfgfd");
address.put("state", "dfgdfgfd");
address.put("country", "India");
address.put("postal_code", "324345");
Serializable reId = s.save("hra_personal_details", emp);
System.out.println("Generated emp_id: "+reId);
//s.save("hra_address", address);
s.persist("hra_address", address);
t.commit();
s.close();
}catch(Throwable t){
t.printStackTrace();
}
}
__________________________________________________________________
Thanking You.
_________________ Gagan Jain
|