-->
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.  [ 9 posts ] 
Author Message
 Post subject: What is wrong ?
PostPosted: Tue May 18, 2004 4:01 pm 
Newbie

Joined: Fri May 14, 2004 9:34 am
Posts: 19
My mappings...

Code:
<hibernate-mapping>
  <class name="Task" table="tasks">
    <id name="id" column="task_id" type="long" unsaved-value="null">
      <generator class="native"/>
    </id>   
    <property name="name" column="task_name" type="string" length="30" not-null="true"/>
    <property name="code" column="task_code" type="string" length="30" not-null="true"/>
    <property name="expirationDate" column="task_expirationDate" type="java.sql.Date" not-null="true"/>
    <set name="taskAttributes" table="taskAttributes" cascade="all" inverse="true" lazy="true">
      <key column="task_id"/>
      <one-to-many class="TaskAttribute"/>
    </set>
  </class>
</hibernate-mapping>


Code:
<hibernate-mapping>
  <class name="TaskAttribute" table="taskAttributes">
     <id name="id" column="taskAttribute_id" type="long" unsaved-value="null">
      <generator class="native"/>
    </id>
    <property name="expirationDate" column="taskAttribute_expirationDate" type="java.sql.Date" not-null="true"/>
    <many-to-one name="task" class="Task" column="task_id" not-null="true" cascade="all"/>
    <one-to-one name="attribute" class="Attribute" column="attribute_id" not-null="true"/>
  </class>
</hibernate-mapping>


Code:
<hibernate-mapping>
  <class name="Attribute" table="attributes">
    <id name="id" column="attribute_id" type="long" unsaved-value="null">   
      <generator class="native"/>
    </id>
    <property name="name" column="attribute_name" type="string" length="30" not-null="true"/>
    <property name="expirationDate" column="attribute_expirationDate" type="java.sql.Date" not-null="true"/>
  </class>
</hibernate-mapping>


I first create an instance of an Attribute save it to the DB using hibernate, then create an instance of Task and pass the attribute as param, so Task creates a TaskAttribute which keep the reference to the attr and the task.
But when i save the Task using hibernate i get ....

Code:
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
        at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
        at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:687)
        at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:640)
        at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
        at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2407)
        at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2361)
        at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2229)
        at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
        at Test.main(Test.java:51)
Exception in thread "main" net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
        at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
        at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:687)
        at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:640)
        at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
        at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2407)
        at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2361)
        at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2229)
        at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
        at Test.main(Test.java:51)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 18, 2004 4:45 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
show java code


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2004 5:38 am 
Senior
Senior

Joined: Fri Nov 21, 2003 5:55 am
Posts: 155
Maybe you have a problem with your mapping files, you don't use a component element for TaskAttribute.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2004 8:51 am 
Newbie

Joined: Fri May 14, 2004 9:34 am
Posts: 19
My java code...

Code:
public class Attribute {

   private long id;
   private String name;
   private Date expirationDate;
   
   public Attribute() {
   }
   
   
   public Date getExpirationDate() {
      return expirationDate;
   }

   public long getId() {
      return id;
   }

   public String getName() {
      return name;
   }

   public void setExpirationDate(Date date) {
      expirationDate = date;
   }

   public void setId(long l) {
      id = l;
   }

   public void setName(String string) {
      name = string;
   }

}



Code:
public class Task {
   
   private long id;
   private String name;
   private String code;
   private Date expirationDate;
   private Set taskAttributes = new TreeSet();
   
   public Task() {
   }
   
   public String getCode() {
      return code;
   }

   public Date getExpirationDate() {
      return expirationDate;
   }

   public long getId() {
      return id;
   }

   public String getName() {
      return name;
   }

   public Set getTaskAttributes() {
      return taskAttributes;
   }

   public void setCode(String string) {
      code = string;
   }

   public void setExpirationDate(Date date) {
      expirationDate = date;
   }

   public void setId(long l) {
      id = l;
   }

   public void setName(String string) {
      name = string;
   }

   public void setTaskAttributes(Set list) {
      taskAttributes = list;
   }
   
   public void addTaskAttribute(Attribute attribute) {
      TaskAttribute taskAttribute = new TaskAttribute();
      taskAttribute.setAttribute(attribute);
      taskAttribute.setTask(this);
      taskAttribute.setExpirationDate(new Date(20040512));
      getTaskAttributes().add(taskAttribute);
   }
}



Code:
public class TaskAttribute {
   
   private long id;
   private Date expirationDate;
   private Attribute attribute;
   private Task task;
   
   public TaskAttribute() {
   }
   
   public Attribute getAttribute() {
      return attribute;
   }

   public Date getExpirationDate() {
      return expirationDate;
   }

   public void setAttribute(Attribute attribute) {
      this.attribute = attribute;
   }

   public void setExpirationDate(Date date) {
      expirationDate = date;
   }

   public long getId() {
      return id;
   }

   public Task getTask() {
      return task;
   }

   public void setId(long l) {
      id = l;
   }

   public void setTask(Task task) {
      this.task = task;
   }

}



Main....

Code:
public class Test {

   public static void main(String [] args) throws Exception {
      
      Task task = new Task();
      task.setName("cucho");
      task.setCode("abc");
      task.setExpirationDate(new Date(20040512));
      
      Attribute attribute = new Attribute();
      attribute.setName("cuchoAttribute");
      attribute.setExpirationDate(new Date(20040512));
      
      task.addTaskAttribute(attribute);
      
      Configuration cfg = new Configuration().addClass(Task.class).addClass(TaskAttribute.class).addClass(Attribute.class);
      SessionFactory sf = cfg.buildSessionFactory();
      Session session = sf.openSession();
      
      Transaction t = session.beginTransaction();
      
      session.save(attribute);
      //session.save(taskAttribute);
      session.flush();
      session.saveOrUpdate(task);
      
      t.commit();
      session.close();
   }
}



Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2004 9:09 am 
Senior
Senior

Joined: Fri Nov 21, 2003 5:55 am
Posts: 155
Try to load an attribute from your db and create a task and a taskattribute linked.
Save the task first and taskattribute then.
Don't forget to flush them.

I have the same problem than you but it's solved now .
My model is composed by a composed-id, maybe try a component element to link object between.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2004 9:10 am 
Senior
Senior

Joined: Fri Nov 21, 2003 5:55 am
Posts: 155
subich wrote:
Try to load an attribute from your db and create a task and a taskattribute linked.
Save the task first and taskattribute then.
Don't forget to flush them.

I have the same problem than you but it's solved now .
My model is composed by a composed-id, maybe try a component element to link object between.

your implementation seems good.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2004 9:17 am 
Newbie

Joined: Fri May 14, 2004 9:34 am
Posts: 19
i could solve the problem, i change session.saveOrUpdate() to session.save() and all worked fine... gonna check out the api now to see what's the real difference.
Thanks !


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2004 9:25 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You have the types on your ids set to "long" which represents the java primitive long type. You then specify the ids unsaved-value as "null". That won't work; a java primitive cannot be null. If you want to keep using the primitives for the id types then set the unsaved-value to some appropriate integer value which represents an unsaved instance (i.e., "0" the way you currently have things setup).


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2004 9:55 am 
Newbie

Joined: Fri May 14, 2004 9:34 am
Posts: 19
ok thanks !


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

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.