-->
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-many relationships
PostPosted: Tue Apr 26, 2005 7:27 am 
Newbie

Joined: Tue Apr 26, 2005 7:12 am
Posts: 1
Hello,

I am trying to write a test class that will insert data in the database. I am using mysql and xdoclet to generate the hibernate mapping. My test class is supposed to insert data in 2 tables: prescription and medicine. These two tables have a many-to-many relationship between them. The code for my test class is as follows:

public class TestPrescription {
private static SessionFactory sf;

public static void configure() throws HibernateException{
sf = new Configuration()
.addClass(com.smarthealth.model.Prescription.class)
.addClass(com.smarthealth.model.Medicine.class)
.buildSessionFactory();
}

public static void main(String [] args){
try{
configure();
Session session = sf.openSession();
Transaction t = session.beginTransaction();
System.out.println("MANY-TO-MANY MAPPING");

Set set = new HashSet();

Medicine m = new Medicine();
m.setDosage("dosage");
m.setDuration("duration");
m.setName("name");
m.setStrength("strength");
m.setType("type");
session.save(m);
set.add(m);

Prescription p = new Prescription();
p.setDate(new Date());
p.setStatus("bought");
p.setMedicines(set);
session.save(p);

t.commit();
session.close();

// Re-load
session = sf.openSession();
t = session.beginTransaction();

p = (Prescription) session.load(Prescription.class, p.getId());
System.out.println("Prescription: " + p.getMedicines());

t.commit();
session.close();
}
catch(HibernateException he){
he.printStackTrace();
}
}
}

When I run my test class, I get the following error message:

(util.JDBCExceptionReporter 57 ) SQL Error: 1216, SQLState: 23000
(util.JDBCExceptionReporter 58 ) Cannot add or update a child row: a foreign key constraint fails
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:179)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:678)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:309)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at com.smarthealth.test.Example.main(Unknown Source)
Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:822)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:172)
... 8 more
(def.AbstractFlushingEventListener 277 ) Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:179)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:678)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:309)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at com.smarthealth.test.Example.main(Unknown Source)
Caused by: java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:822)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:172)
... 8 more

I read in other forums that the problem might be caused by the id generator being a primitive type (int or long). My id generator is "uuid.hex".

I would be grateful if anyone could help.

Thanks in advance.


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.