-->
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.  [ 5 posts ] 
Author Message
 Post subject: Inserting values ...
PostPosted: Fri May 14, 2004 9:47 am 
Newbie

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

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

   public Date getExpirationDate() {
      return expirationDate;
   }

   public long getId() {
      return id;
   }

   public String getName() {
      return name;
   }

   public List 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(List list) {
      taskAttributes = list;
   }
}


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;
   }

}


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;
   }

}


My Test main class...

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));
      
      TaskAttribute taskAttribute = new TaskAttribute();
      taskAttribute.setExpirationDate(new Date(20040512));
      taskAttribute.setAttribute(attribute); 
      taskAttribute.setTask(task);
      
      List taskAttributes = new ArrayList();
      taskAttributes.add(taskAttribute);
      task.setTaskAttributes(taskAttributes);      
            
      Configuration cfg = new Configuration().addClass(Task.class).addClass(TaskAttribute.class).addClass(Attribute.class);
      SessionFactory sf = cfg.buildSessionFactory();
      Session session = sf.openSession();
      System.out.println("Saving instance");
      session.saveOrUpdate(task);            
      System.out.println("Instance just saved");
   }
}


My mapping files...

Quote:
<hibernate-mapping>
<class name="Task" table="tasks">
<id name="id" column="task_id" type="long" unsaved-value="null">
<generator class="hilo"/>
</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>


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


Quote:
<hibernate-mapping>
<class name="Attribute" table="attributes">
<id name="id" column="attribute_id" type="long" unsaved-value="null">
<generator class="hilo"/>
</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>


and the output when I run the test...

Quote:
blah blah... everything ok...
10:24:35,303 INFO SessionFactoryImpl:119 - building session factory
10:24:37,114 INFO SessionFactoryObjectFactory:82 - no JNDI name configured
10:24:37,150 INFO UpdateTimestampsCache:35 - starting update timestamps cache at region: net.sf.hibernate.cache.UpdateTimestampsCache
10:24:37,255 WARN Configurator:123 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/home/v910312/hibernateWorkspace/HibernateTest/lib/ehcache-0.7.jar!/ehcache-failsafe.xml
10:24:37,342 WARN Plugin:95 - Could not find configuration for net.sf.hibernate.cache.UpdateTimestampsCache. Configuring using the defaultCache settings.
10:24:37,446 INFO QueryCache:39 - starting query cache at region: net.sf.hibernate.cache.QueryCache
10:24:37,449 WARN Plugin:95 - Could not find configuration for net.sf.hibernate.cache.QueryCache. Configuring using the defaultCache settings.
Saving instance
Instance just saved


so... everything seems to be ok, i can connect to the db and create the tables using SchemeExport, etc, etc but when i check the tables in the database after executing the Test.java nothing is saved... they are all empty.
I'm not a DB specialist, so i neither don't know if the db desing is ok.
Thanks all for any possible hand !


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 9:56 am 
Regular
Regular

Joined: Wed Apr 21, 2004 10:57 am
Posts: 62
Location: Hasselt, Belgium
You haven't commit your data.

try to add

Transaction t = session.beginTransaction();
System.out.println("Saving instance");
session.saveOrUpdate(task);
System.out.println("Instance just saved");
t.commit();


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 10:10 am 
Newbie

Joined: Fri May 14, 2004 9:34 am
Posts: 19
thanks... now throws

Quote:
Exception in thread "main" java.lang.ClassCastException
at net.sf.hibernate.type.SetType.wrap(SetType.java:24)
at net.sf.hibernate.impl.WrapVisitor.processArrayOrNewCollection(WrapVisitor.java:78)
at net.sf.hibernate.impl.WrapVisitor.processCollection(WrapVisitor.java:49)
at net.sf.hibernate.impl.AbstractVisitor.processValue(AbstractVisitor.java:69)
at net.sf.hibernate.impl.WrapVisitor.processValues(WrapVisitor.java:93)
at net.sf.hibernate.impl.SessionImpl.flushEntity(SessionImpl.java:2481)
at net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2447)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:2249)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2228)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at Test.main(Test.java:45)


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 10:21 am 
Regular
Regular

Joined: Wed Apr 21, 2004 10:57 am
Posts: 62
Location: Hasselt, Belgium
k, I think I found it.

You use a Set in your mapping file but a List in your Java file.
A Set cannot be formed into a List directly.

2 possible solutions
- Use a Set taskAttributes instead of List taskAttributes in Task.
- Change it in your mapping file, but then you will need an index field too.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 10:41 am 
Newbie

Joined: Fri May 14, 2004 9:34 am
Posts: 19
ok thanks a lot, I just statirng out with this framework yesterday.
I thought set was a default keywork for every Collection... i'll keep googlin
THanks !


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.