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.  [ 8 posts ] 
Author Message
 Post subject: Problems retrieving a Set...
PostPosted: Fri Dec 08, 2006 11:04 am 
Newbie

Joined: Tue Oct 03, 2006 9:22 am
Posts: 11
Hey all,

I'm playing around trying to learn the hibernate annotations, and have stumbled upon a problem (not annotations-related)

I have an object with a Set of properties, like this :
Code:
@Entity
@Table(name = "OBJECT")
public class DataObject {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private long id;

   @Column
   private String name;

   @ManyToMany(cascade = { CascadeType.ALL })
   private Set<Property> properties;

   @ManyToOne
   private DataObject parent;
   
   @ManyToMany(cascade = { CascadeType.ALL })
   private List<DataObject> children;
   
   public long getId() {
      return id;
   }

   public void setId(long id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public Set<Property> getProperties() {
      return properties;
   }

   public void setProperties(Set<Property> properties) {
      this.properties = properties;
   }

   public List<DataObject> getChildren() {
      return children;
   }

   public void setChildren(List<DataObject> children) {
      this.children = children;
   }

}

@Entity
@Inheritance (strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Property {

   @Id @GeneratedValue(strategy=GenerationType.AUTO)
   private long id = -1;

   @ManyToOne
   private PropertyType propertytype;
   
   public long getId() {
      return id;
   }

   public void setId(long id) {
      this.id = id;
   }
   
   public PropertyType getPropertytype() {
      return propertytype;
   }

   public void setPropertytype(PropertyType propertytype) {
      this.propertytype = propertytype;
   }

   public abstract void setValue(Object o);
   
   public abstract Object getValue();
   
}

@Entity @Table(name = "TEXTPROPERTY")
public class TextProperty extends Property {

   @Column @Type(type="org.hibernate.type.StringClobType")
   private String value;

   public Object getValue() {
      return value;
   }

   public void setValue(Object value) {
      this.value = (String) value;
   }
   
   
}

@Entity
@Table (name = "VARCHARPROP")
public class VarcharProperty extends Property {

   private String value;

   public Object getValue() {
      return value;
   }

   public void setValue(Object value) {
      this.value = (String) value;
   }
   
   
}


Storing an Object with 1 TextProperty and 1 VarcharProperty goes just fine, but my problem is that when I try to access the properties Set on the DataObject afterwards I get an SQL exception: "expression must have same datatype as corresponding expression". The resulting sql contains the following snippet, which causes the exception :
Code:
select value, propertytype_id, id, 1 as clazz_ from TEXTPROPERTY union all select value, propertytype_id, id, 2 as clazz_ from VARCHARPROP

which fails because value from textproperty is a clob and value from varcharprop is a varchar.

Am I doing something wrong, or is it just not possible to do this when using Hibernate ?

Using Hibernate 3.2.0[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 11:21 am 
Newbie

Joined: Thu Dec 07, 2006 1:02 pm
Posts: 10
When using a union, the datatypes have to be the same


Top
 Profile  
 
 Post subject: Yes
PostPosted: Fri Dec 08, 2006 11:36 am 
Newbie

Joined: Tue Oct 03, 2006 9:22 am
Posts: 11
I know, but the problem is that the generated sql is hibernate generated, none of my doing, so either I'm tyring to use hibernate in a way it wasnt meant to be used or I have detected a problem (not likely if you ask me..)


Top
 Profile  
 
 Post subject: Re: Yes
PostPosted: Fri Dec 08, 2006 11:56 am 
Newbie

Joined: Thu Dec 07, 2006 1:02 pm
Posts: 10
d0h wrote:
I know, but the problem is that the generated sql is hibernate generated, none of my doing, so either I'm tyring to use hibernate in a way it wasnt meant to be used or I have detected a problem (not likely if you ask me..)


Can you post your mappings for the tables?? In your mapping you might have to explicitly tell hibernate which datatypes you want to use...


Top
 Profile  
 
 Post subject: Re: Yes
PostPosted: Fri Dec 08, 2006 12:14 pm 
Newbie

Joined: Thu Dec 07, 2006 1:02 pm
Posts: 10
d0h wrote:
I know, but the problem is that the generated sql is hibernate generated, none of my doing, so either I'm tyring to use hibernate in a way it wasnt meant to be used or I have detected a problem (not likely if you ask me..)


Taken from the tutorial:
Finally we include declarations for the persistent properties of the class in the mapping file. By default, no properties of the class are considered persistent:

Code:
<hibernate-mapping>

    <class name="events.Event" table="EVENTS">
        <id name="id" column="EVENT_ID">
            <generator class="native"/>
        </id>
        <property name="date" type="timestamp" column="EVENT_DATE"/>
        <property name="title"/>
    </class>

</hibernate-mapping>



Code:
Just as with the id element, the name attribute of the property element tells Hibernate which getter and setter methods to use. So, in this case, Hibernate will look for getDate()/setDate(), as well as getTitle()/setTitle().

Why does the date property mapping include the column attribute, but the title doesn't? Without the column attribute Hibernate by default uses the property name as the column name. This works fine for title. However, date is a reserved keyword in most database, so we better map it to a different name.

The next interesting thing is that the title mapping also lacks a type attribute. The types we declare and use in the mapping files are not, as you might expect, Java data types. They are also not SQL database types. These types are so called Hibernate mapping types, converters which can translate from Java to SQL data types and vice versa. Again, Hibernate will try to determine the correct conversion and mapping type itself if the type attribute is not present in the mapping. In some cases this automatic detection (using Reflection on the Java class) might not have the default you expect or need. This is the case with the date property. Hibernate can't know if the property (which is of java.util.Date) should map to a SQL date, timestamp, or time column. We preserve full date and time information by mapping the property with a timestamp converter.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 12:42 pm 
Newbie

Joined: Tue Oct 03, 2006 9:22 am
Posts: 11
Well, I dont have any mapping files, since I'm using the annotations that you can see in the code from my original post.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 1:20 pm 
Newbie

Joined: Thu Dec 07, 2006 1:02 pm
Posts: 10
d0h wrote:
Well, I dont have any mapping files, since I'm using the annotations that you can see in the code from my original post.


well instead of saying "GenerationType.AUTO" try setting the type yourself or go into the actual database and change the type there


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 1:56 pm 
Newbie

Joined: Tue Oct 03, 2006 9:22 am
Posts: 11
Change it to what ? GenerationType.AUTO only makes hibernate use a sequence (I'm using an oracle database) for generating an id for my entity, so without sounding too sure, then I'm pretty sure that that it not my problem, since hibernate can store the properties.


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