-->
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.  [ 1 post ] 
Author Message
 Post subject: get last insert id from one table and put in to another tabl
PostPosted: Sat Apr 03, 2010 1:47 am 
Newbie

Joined: Wed Mar 31, 2010 4:35 am
Posts: 19
Hi

I have two tables
Xydataa, Fm4featuress

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

Code:

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


import java.sql.Timestamp;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

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


     private Integer id;
     private String longi;
     private String lat;
     private int altitude;
     private int angle;
     private int satellite;
     private int speed;
     private int objectId;
     private int clientId;
     private String gpsTime;
     private String location;
     private String city;
     private String state;
     private String distance;
     private Timestamp times;
     private Set fm4featuresses = new HashSet();

    public Xydataa() {
    }

   
    public Xydataa(int altitude, int angle, int satellite, int speed, int objectId, int clientId, String gpsTime, Timestamp times) {
        this.altitude = altitude;
        this.angle = angle;
        this.satellite = satellite;
        this.speed = speed;
        this.objectId = objectId;
        this.clientId = clientId;
        this.gpsTime = gpsTime;
        this.times = times;
    }
    public Xydataa(String longi, String lat, int altitude, int angle, int satellite, int speed, int objectId, int clientId, String gpsTime, String location, String city, String state, String distance, Timestamp times, Set fm4featuresses) {
       this.longi = longi;
       this.lat = lat;
       this.altitude = altitude;
       this.angle = angle;
       this.satellite = satellite;
       this.speed = speed;
       this.objectId = objectId;
       this.clientId = clientId;
       this.gpsTime = gpsTime;
       this.location = location;
       this.city = city;
       this.state = state;
       this.distance = distance;
       this.times = times;
       this.fm4featuresses = fm4featuresses;
    }
   
    public Integer getId() {
        return this.id;
    }
   
    public void setId(Integer id) {
        this.id = id;
    }
    public String getLongi() {
        return this.longi;
    }
   
    public void setLongi(String longi) {
        this.longi = longi;
    }
    public String getLat() {
        return this.lat;
    }
   
    public void setLat(String lat) {
        this.lat = lat;
    }
    public int getAltitude() {
        return this.altitude;
    }
   
    public void setAltitude(int altitude) {
        this.altitude = altitude;
    }
    public int getAngle() {
        return this.angle;
    }
   
    public void setAngle(int angle) {
        this.angle = angle;
    }
    public int getSatellite() {
        return this.satellite;
    }
   
    public void setSatellite(int satellite) {
        this.satellite = satellite;
    }
    public int getSpeed() {
        return this.speed;
    }
   
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public int getObjectId() {
        return this.objectId;
    }
   
    public void setObjectId(int objectId) {
        this.objectId = objectId;
    }
    public int getClientId() {
        return this.clientId;
    }
   
    public void setClientId(int clientId) {
        this.clientId = clientId;
    }
    public String getGpsTime() {
        return this.gpsTime;
    }
   
    public void setGpsTime(String gpsTime) {
        this.gpsTime = gpsTime;
    }
    public String getLocation() {
        return this.location;
    }
   
    public void setLocation(String location) {
        this.location = location;
    }
    public String getCity() {
        return this.city;
    }
   
    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return this.state;
    }
   
    public void setState(String state) {
        this.state = state;
    }
    public String getDistance() {
        return this.distance;
    }
   
    public void setDistance(String distance) {
        this.distance = distance;
    }
    public Timestamp getTimes() {
        return this.times;
    }
   
    public void setTimes(Timestamp times) {
        this.times = times;
    }
    public Set getFm4featuresses() {
        return this.fm4featuresses;
    }

    public void addFm4featuresses(Fm4featuress fm4featuress) {
      this.fm4featuresses.add(fm4featuress);
   }

    public void setFm4featuresses(Set fm4featuresses) {
        this.fm4featuresses = fm4featuresses;
    }


Fm4featuress fm=new Fm4featuress();
   @Override
public boolean equals(Object fm) {
        if (this == fm) return true;
        if ( !(fm instanceof Xydataa) )return false;

        final Xydataa xy = (Xydataa) fm;
     if (null != id ? !id.equals(xy.getId()) : null != xy.getId())
            return false;
        return true;

    }

    @Override
    public int hashCode() {
       return null != id ? id.hashCode() : super.hashCode();
    }


}



Code:
Fm4featuress
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
Code:
{
    //    PreparedStatement preparedStatement = null;
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        DecimalNumber xx ;
        DecimalNumber yy ;
        Session sess = null;
        Transaction tr = null;
        sess = HibernateUtil.getSessionFactory().openSession();
             Date resultdate = new Date(times);
             xx = new DecimalNumber(x,7);
              yy = new DecimalNumber(y,7);
              System.out.println("Inserting xydataa");
              Xydataa xy=new Xydataa();
              Fm4featuress fm = new Fm4featuress();
              Set set = new HashSet();
              xy.setLongi(xx+"");
              xy.setLat(yy+"");
              xy.setAltitude(altitude);
              xy.setAngle(angle);
              xy.setSatellite(satellites);
              xy.setSpeed(speed);
              xy.setObjectId(objectId);
              xy.setClientId(clientId);
              xy.setGpsTime(sdf.format(resultdate));
              xy.setLocation(location);
              xy.setCity(city);
              xy.setState(state);
              xy.setDistance(distance);
              System.out.println("Done");
             
              for (int i = 0; i < obj.length && obj[i] != 0; i++)
             {
              int all = obj[i] - 1;
              System.out.println("Inserting Fm4Featuress");
             // int b=xy.getId();
              //fm.setId(xy.getId());
              fm.setDataId(all);
              fm.setValue(obja[i]);
              fm.setXydataa(xy);
              xy.getFm4featuresses().add(fm);
              set.add(fm);
              xy.addFm4featuresses(fm);
              }
           Transaction transaction = null;
          try {
           transaction = sess.beginTransaction();
           sess.save(xy);
           //sess.save(fm);
           //transaction.commit();
           System.out.println("Transaction commited");
      } catch (Exception e) {
         System.out.println("Exception Here");
           if (transaction != null) {
             transaction.rollback();
             throw e;
           }
      }  finally {
           sess.close();
      }
             
   }



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


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

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.