aspose file tools*
The moose likes Object Relational Mapping and the fly likes How to get last insert id from one table and put in to the another table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to get last insert id from one table and put in to the another table" Watch "How to get last insert id from one table and put in to the another table" New topic
Author

How to get last insert id from one table and put in to the another table

Baktha Elumalai
Greenhorn

Joined: Mar 13, 2010
Posts: 28
Hi

I have two tables
Xydataa, Fm4featuress

Mapping file
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 2, 2010 3:36:14 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="pip.Xydataa" table="xydataa" catalog="navl">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="identity" />
</id>
<property name="longi" type="string">
<column name="Longi" />
</property>
<property name="lat" type="string">
<column name="Lat" />
</property>
<property name="altitude" type="int">
<column name="Altitude" not-null="true" />
</property>
<property name="angle" type="int">
<column name="Angle" not-null="true" />
</property>
<property name="satellite" type="int">
<column name="Satellite" not-null="true" />
</property>
<property name="speed" type="int">
<column name="Speed" not-null="true" />
</property>
<property name="objectId" type="int">
<column name="ObjectId" not-null="true" />
</property>
<property name="clientId" type="int">
<column name="ClientId" not-null="true" />
</property>
<property name="gpsTime" type="string">
<column name="GpsTime" length="19" not-null="true" />
</property>
<property name="location" type="string">
<column name="Location" />
</property>
<property name="city" type="string">
<column name="City" />
</property>
<property name="state" type="string">
<column name="State" />
</property>
<property name="distance" type="string">
<column name="Distance" length="100" />
</property>
<property name="times" type="timestamp">
<column name="Times" length="19" not-null="false" />
</property>
<set name="fm4featuresses" inverse="true">
<key>
<column name="ID" not-null="true" unique="true" />
</key>
<one-to-many class="pip.Fm4featuress" />
</set>
</class>
</hibernate-mapping>

Fm4featuress.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 2, 2010 3:36:14 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="pip.Fm4featuress" table="fm4featuress" catalog="navl">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<many-to-one name="xydataa" class="pip.Xydataa" update="false" insert="false" fetch="select">
<column name="ID" not-null="true" unique="true" />
</many-to-one>
<property name="dataId" type="java.lang.Integer">
<column name="DataId" />
</property>
<property name="value" type="java.lang.Integer">
<column name="Value" />
</property>
</class>
</hibernate-mapping>

Pojo file


and

Fm4featuress.java

package pip;
// Generated Apr 2, 2010 3:36:13 PM by Hibernate Tools 3.2.1.GA



/**
* Fm4featuress generated by hbm2java
*/
public class Fm4featuress implements java.io.Serializable {


private int id;
private Xydataa xydataa;
private Integer dataId;
private Integer value;

public Fm4featuress() {
}


public Fm4featuress(int id, Xydataa xydataa) {
this.id = id;
this.xydataa = xydataa;
}
public Fm4featuress(int id, Xydataa xydataa, Integer dataId, Integer value) {
this.id = id;
this.xydataa = xydataa;
this.dataId = dataId;
this.value = value;
}

public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}
public Xydataa getXydataa() {
return this.xydataa;
}

public void setXydataa(Xydataa xydataa) {
this.xydataa = xydataa;
}
public Integer getDataId() {
return this.dataId;
}

public void setDataId(Integer dataId) {
this.dataId = dataId;
}
public Integer getValue() {
return this.value;
}

public void setValue(Integer value) {
this.value = value;
}




}


this is the main file




Here i am using one to many->Xydataa-Fm4featuress
Many to One->Fm4featuress-Xydataa

i got the value from xydataa but in Fm4featuress only print id as 0, dataid as 1 &2, Value as 0

What i am wrong in the code

How to get the last insert id from the xydataa and print in to the fm4featuress

Please help me it's very urgent.


Regards
Bakthavachalam E
Reehan Lalkhanwar
Ranch Hand

Joined: Jun 16, 2007
Posts: 106
I think, as Xydataa has a generator class of identity, you will have to save it so that the ID is generated and use that ID for assignment in Fm4featuress.
You may also try out setting the ID of Fm4featuress.


Thank you,
Reehan
Baktha Elumalai
Greenhorn

Joined: Mar 13, 2010
Posts: 28
Hi Reehan

I have changes the same before you sent it,

I have changes some thing now it will work but not exactly

this is our changes code

Xydataa.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 2, 2010 3:36:14 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="pip.Xydataa" table="xydataa" catalog="navl">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="identity" />
</id>
<property name="longi" type="string">
<column name="Longi" />
</property>
<property name="lat" type="string">
<column name="Lat" />
</property>
<property name="altitude" type="int">
<column name="Altitude" not-null="true" />
</property>
<property name="angle" type="int">
<column name="Angle" not-null="true" />
</property>
<property name="satellite" type="int">
<column name="Satellite" not-null="true" />
</property>
<property name="speed" type="int">
<column name="Speed" not-null="true" />
</property>
<property name="objectId" type="int">
<column name="ObjectId" not-null="true" />
</property>
<property name="clientId" type="int">
<column name="ClientId" not-null="true" />
</property>
<property name="gpsTime" type="string">
<column name="GpsTime" length="19" not-null="true" />
</property>
<property name="location" type="string">
<column name="Location" />
</property>
<property name="city" type="string">
<column name="City" />
</property>
<property name="state" type="string">
<column name="State" />
</property>
<property name="distance" type="string">
<column name="Distance" length="100" />
</property>
<property name="times" type="timestamp">
<column name="Times" length="19" not-null="false" />
</property>
<set name="fm4featuresses" inverse="false">
<key>
<column name="ID" not-null="true"/>
</key>
<one-to-many class="pip.Fm4featuress" />
</set>
</class>
</hibernate-mapping>


Fm4featuress

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 2, 2010 3:36:14 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="pip.Fm4featuress" table="fm4featuress" catalog="navl">
<id name="id" type="int" unsaved-value="null">
<column name="ID" />
<generator class="assigned" />
</id>
<many-to-one name="xydataa" class="pip.Xydataa" update="false" insert="false" fetch="select">
<column name="ID" not-null="true"/>
</many-to-one>
<property name="dataId" type="java.lang.Integer">
<column name="DataId" not-null="true"/>
</property>
<property name="value" type="java.lang.Integer">
<column name="Value" not-null="true"/>
</property>
</class>
</hibernate-mapping>


Pojo Class






This is the main file


i need this type of output

Xydataa table

Id a b
1 45 56
2 56 78
3 123 456
4 345 678

Fm4features Table

Id
1
1
1
2
2
2
2
3
3
3

like this what i am doing wrong please reply
Reehan Lalkhanwar
Ranch Hand

Joined: Jun 16, 2007
Posts: 106
The id field of Fm4featuress has been declared as a id which is equivalent to a key field in the database.
So I don't think that the sort of entries you wish to have, which includes duplicates, cannot be done unless you make ID a non id field.
Baktha Elumalai
Greenhorn

Joined: Mar 13, 2010
Posts: 28
In Xydataa table

ID-PK

i am using one(ID)-Many(ID) and Many-One releationship

and also using composite key(ID, DataId)

Hbm File

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 8, 2010 4:08:07 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="test.Xydataa" table="xydataa" catalog="navl">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="identity" />
</id>
<property name="longi" type="string">
<column name="Longi" />
</property>
<property name="lat" type="string">
<column name="Lat" />
</property>
<property name="altitude" type="int">
<column name="Altitude" not-null="true" />
</property>
<property name="angle" type="int">
<column name="Angle" not-null="true" />
</property>
<property name="satellite" type="int">
<column name="Satellite" not-null="true" />
</property>
<property name="speed" type="int">
<column name="Speed" not-null="true" />
</property>
<property name="objectId" type="int">
<column name="ObjectId" not-null="true" />
</property>
<property name="clientId" type="int">
<column name="ClientId" not-null="true" />
</property>
<property name="gpsTime" type="string">
<column name="GpsTime" length="19" not-null="false" />
</property>
<property name="location" type="string">
<column name="Location" />
</property>
<property name="city" type="string">
<column name="City" />
</property>
<property name="state" type="string">
<column name="State" />
</property>
<property name="distance" type="string">
<column name="Distance" length="100" />
</property>
<property name="times" type="timestamp">
<column name="Times" length="19" not-null="false" />
</property>
<set name="objtablees" inverse="true">
<key>
<column name="ID" not-null="true" unique="true" />
</key>
<one-to-many class="test.Objtablee" />
</set>
<set name="fm4featuresses" cascade="all" inverse="true">
<key>
<column name="ID" not-null="true" />
</key>
<one-to-many class="test.Fm4featuress" />
</set>
</class>
</hibernate-mapping>


Fm4Featuress

ID-PK-FK
DataId-Pk


Hbm File

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 8, 2010 4:08:07 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="test.Fm4featuress" table="fm4featuress" catalog="navl">
<composite-id name="id" class="test.Fm4featuressId">
<key-many-to-one name="id" class="test.Xydataa" column="ID" />
<key-property name="dataId" column="DataId"/>
</composite-id>
<property name="value" type="string">
<column name="Value" length="20" not-null="true" />
</property>
</class>
</hibernate-mapping>



but now i am getting the following error

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): test.Fm4featuress
at org.hibernate.id.Assigned.generate(Assigned.java:33)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:99)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:334)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at tcplistener.Alldatabase.Database(Alldatabase.java:153)
at tcplistener.ModuleHandler.run(ModuleHandler.java:155)
at java.lang.Thread.run(Thread.java:619)
INFO: Listening on ...... 4000
INFO: Hibernate: select objectinfo0_.ObjectId as ObjectId5_, objectinfo0_.ClientId as ClientId5_, objectinfo0_.Comments as Comments5_, objectinfo0_.CompanyName as CompanyN4_5_, objectinfo0_.Device as Device5_, objectinfo0_.DeviceSerial as DeviceSe6_5_, objectinfo0_.DisplayName as DisplayN7_5_, objectinfo0_.Email1 as Email8_5_, objectinfo0_.Email2 as Email9_5_, objectinfo0_.Email3 as Email10_5_, objectinfo0_.Email4 as Email11_5_, objectinfo0_.GSMNUmber as GSMNUmber5_, objectinfo0_.IMEI as IMEI5_, objectinfo0_.Identifier as Identifier5_, objectinfo0_.Mobile1 as Mobile15_5_, objectinfo0_.Mobile2 as Mobile16_5_, objectinfo0_.Mobile3 as Mobile17_5_, objectinfo0_.Mobile4 as Mobile18_5_, objectinfo0_.ObjectCode as ObjectCode5_, objectinfo0_.Operator as Operator5_, objectinfo0_.Password as Password5_, objectinfo0_.Pin as Pin5_, objectinfo0_.Puk as Puk5_, objectinfo0_.SimSerial as SimSerial5_, objectinfo0_.Time as Time5_, objectinfo0_.date as date5_ from objectinfo objectinfo0_ where objectinfo0_.IMEI='353976013426923'
INFO: Module IMEI :: ::353976013426923 ObjectId 18
INFO: Packet Recieved Time :: :: Apr 9, 2010 12:17:04 PM: Received records:30
INFO: Inserting xydataa
INFO: Done
INFO: Inserting Fm4Featuress
INFO: true
INFO: ***ID=0***DataId=1***Value=0
INFO: Inserting Fm4Featuress
INFO: true
INFO: ***ID=0***DataId=2***Value=0
INFO: Hibernate: insert into navl.xydataa (Longi, Lat, Altitude, Angle, Satellite, Speed, ObjectId, ClientId, GpsTime, Location, City, State, Distance, Times) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
INFO: Exception Here
SEVERE: null
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): test.Fm4featuress
at org.hibernate.id.Assigned.generate(Assigned.java:33)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:99)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:334)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)


What i am doing wrong please help me,
Advance Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get last insert id from one table and put in to the another table
 
Similar Threads
org.hibernate.QueryException: could not resolve property: ObjectId of: test.Objtablee
Field 'ID' doesn't have a default value: org.hibernate.exception.GenericJDBCException: could not ins
org.hibernate.exception.GenericJDBCException: could not insert:
ids for this class must be manually assigned before calling save()
Cannot add or update a child row: a foreign key constraint fails