Hi
I have further downsized my example, and it inserts new rows into mesgtype.
If I run the program twice, it will insert the a new row of mesgtype the second time with incremented oid. This is not my plan. The number of rows of the mesgtype should not be increased everytime the job is added. Please tell me how to fix it.
Job.java public class Job { private long oid; private Mesgtype mesgtype; long getOid() { return oid; } void setOid(long _oid) { oid=_oid; } Mesgtype getMesgtype() { return mesgtype; } void setMesgtype(Mesgtype _mesgtype) { mesgtype=_mesgtype; } }
job.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"> <hibernate-mapping package="roseindia.tutorial.hibernate"> <class name="Job" table="JOB"> <id name="oid" type="long" column="oid" > <generator class="increment"/> </id> <many-to-one class="Mesgtype" name="mesgtype"/> </class> </hibernate-mapping>
MesgType.java public class Mesgtype { long oid; long getOid() { return oid; } void setOid(long _oid) { oid=_oid; } } MesgType.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"> <!-- oid int(10) NOT NULL category varchar(30) NULL --> <hibernate-mapping package="com.jobscout.frontpage.client"> <class name="Mesgtype" table="MESGTYPE"> <id name="oid" type="long" column="oid" > <generator class="assigned"/> </id> <property name="mesgtype"> <column name="mesgtype" /> </property> </class> </hibernate-mapping>
Main.java public class Main { public static void main(String[] args) { Session session = null; try{ // This step will read hibernate.cfg.xml and prepare hibernate for use SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); session =sessionFactory.openSession(); //Transaction tx=session.beginTransaction(); //Create new instance of Contact and set values in it by reading them from form object System.out.println("Inserting Record"); Mesgtype mesgtype1 = new Mesgtype(); mesgtype1.setOid(1); Job job1=new Job(); job1.setOid(1); job1.setMesgtype(mesgtype1); Job job2=new Job(); job2.setOid(2); job2.setMesgtype(mesgtype1); Job job3=new Job(); job3.setOid(3); job3.setMesgtype(mesgtype1); session.save(mesgtype1); session.save(job1); session.save(job2); session.save(job3); System.out.println("Done"); session.flush(); //tx.commit(); session.close(); }catch(Exception e){ e.printStackTrace(); } } } Thanks
|