-->
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.  [ 2 posts ] 
Author Message
 Post subject: problems mapping Java-classes to database
PostPosted: Wed Jun 08, 2005 6:42 am 
Newbie

Joined: Mon Jun 06, 2005 8:21 am
Posts: 13
Hibernate 2


MySql 4.1.12 for Win32



Hello!

I have simplified the Person class a bit (that is, I removed some stuff like age, sex etc).

A person class contains several standard data like age, sex and also optional data that can be provided. The optional data is in key-value form (email, ddd@ddd.fi), (address, Sunset Boulevard 132) etc

This data is submitted using a web form.

I collect the submitted key-value pairs and populate a new Option object with those values. I then put all created Option objects in a Set, which I finally sotre in the Person object (using method setOption(Set) ).

The idea is that as I use hibernate to store the Person object to MySql database, it should also store the Option objects (which reside in Person class) in Option table.

I'm using Spring's getHibernateTemplate().save() for saving data.

As I understand it, this should be doable.

I guess the problem resides in my hibernate mapping file. I don't quite figure out how to accomplish this.


Here's my classes:

Code:

public class Person
{
   private java.util.Set option;


   public Person() {}

   public void setOption(Set set) { option = set; }

   public java.util.Set getOption() { return option; }
}

public class Option
{
   private Id id;

   private String value;


   public Option() {}

   public void setId(Id id) { this.id = id; }
   public void setValue(String val) { value = val; }

   public Id getId() { return id; }
   public String getValue() { return value; }


   public boolean equals(Object o) { // implementation }

   public int hashCode() { // implementation }

}

public class Id
{
   private int id;

   private String key;


   public Id() {}

   public void setId(int id) { this.id = id; }
   public void setKey(String key) {this.key = key; }

   public int getId() { return id; }
   public String getKey() { return key; }
}




Here's my hibernate mappings:

Code:

   <class name="Person" class="com.jme.Person">
       <id name="id" type="string" unsaved-value="null" >
               <column name="id" sql-type="varchar(4)" not-null="true"/>
               <generator class="native" />
            </id>   
           
       <set name="options">
          <key column="id" />
      <one-to-many class="Options" />
       </set>
           
   </class>
   
   
   <class name="Options" table="Options">
      <composite-id name="id" class="com.jme.Id">
         <key-property name="id" column="id" />
         <key-property name="opt_key" column="opt_key" />
      </composite-id>
   
      <property name="opt_value" type="string" not-null="true">
         <column name="opt_value" sql-type="text" />
      </property>
   </class>   
   



[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 1:17 am 
Beginner
Beginner

Joined: Mon Jun 13, 2005 5:52 pm
Posts: 43
The name attributes need to refer to properies (getters and setters) on your classes--not database column names (e.g. opt_key).


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