-->
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: many to one foreign key - unexpected insert to the table
PostPosted: Sun Jun 06, 2010 7:08 am 
Newbie

Joined: Sun Apr 11, 2010 6:44 am
Posts: 12
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


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.