-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: UserType with annotation
PostPosted: Wed Mar 02, 2005 8:02 am 
Beginner
Beginner

Joined: Mon Dec 15, 2003 5:25 am
Posts: 48
Location: Delhi, India
Hi All,

Is there any example/docs to use UserType with annotations?

Thanks,

_________________
Vinod K. Singh


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 04, 2005 3:32 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
no doc yet.
Just use
Code:
@Type(type="mytype")

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 8:20 am 
Beginner
Beginner

Joined: Mon Dec 15, 2003 5:25 am
Posts: 48
Location: Delhi, India
Thanks Emmanuel, it is working, but I ran into another problem.

My class model is like below

Code:
@Entity
public class Style
{
  private String name;
  private String type;

  @Type(type="Style_Def")
  private String definition;
}

public class Style_Def implements UserType
{
  private String name;
  private String type;
 
  public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException
  {
    String xml = rs.getString(names[0]);
    if (!rs.wasNull())
    {
      Style style = (Style) owner;
      if(style.getType().equals("COLOR"))
        return StyleFactory.createColorStyle(xml);
      else if(style.getType().equals("TEXT"))
        return StyleFactory.createTextStyle(xml);
    }
  }
}

public class Color extends Style_Def
{
  private String stroke;
  private int strokeWidth;
  private String fill;
}

public class Text extends Style_Def
{
  private String family;
  private int size;
}


I am able to save the objects in database by using above structure but at the retrieval time I am getting NPE at the following line-

Code:
if(style.getType().equals("COLOR"))


because Style object is not populated by the time nullSafeGet is called on Style_Def.

While the query used by Hibernate is fetching all data and Style.type fields has NOT NULL condition.

Is there any wayout for the above problem.

Thanks,

_________________
Vinod K. Singh


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2005 9:53 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you might play with the recordset to find type.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2005 10:46 am 
Beginner
Beginner

Joined: Mon Dec 15, 2003 5:25 am
Posts: 48
Location: Delhi, India
emmanuel wrote:
you might play with the recordset to find type.


I tried that but unable to find the value of property type. As hibernate generates query with arbitrary column names like below-

Code:
select style0_.name as name0_, style0_.definition as definition9_0_, style0_.user_id as user6_9_0_,
style0_.type as type9_0_, style0_.image as image9_0_, style0_.description as descript5_9_0_
from Style style0_ where style0_.name='Color 1'


The ResultSetMetaData.getColumnLabel(int) and ResultSetMetaData.getColumnName(int) return TYPE9_0_ for type property in ResultSet.

Can you confirm me one thing that the column aliases used in generated sql by hibernate are always started with property name (this is what I noticed) and this behaviour is not going to change in future releases also.

If this is true then I can always find value of any given property form the ResultSet.

Thanks for response.

_________________
Vinod K. Singh


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 11, 2005 11:47 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
vinodsingh wrote:
Can you confirm me one thing that the column aliases used in generated sql by hibernate are always started with property name (this is what I noticed) and this behaviour is not going to change in future releases also.

No, we want to keep the freedom to change this, sorry :-/

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 14, 2005 1:05 am 
Beginner
Beginner

Joined: Mon Dec 15, 2003 5:25 am
Posts: 48
Location: Delhi, India
emmanuel wrote:
vinodsingh wrote:
Can you confirm me one thing that the column aliases used in generated sql by hibernate are always started with property name (this is what I noticed) and this behaviour is not going to change in future releases also.

No, we want to keep the freedom to change this, sorry :-/


Then tell what is the best way to play with ResultSet?

Thanks,

_________________
Vinod K. Singh


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 14, 2005 5:36 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Please stop posting your questions on our blog as well. Stay here in the forum or get a support contract if you have time constraints.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 14, 2005 5:44 am 
Beginner
Beginner

Joined: Mon Dec 15, 2003 5:25 am
Posts: 48
Location: Delhi, India
christian wrote:
Please stop posting your questions on our blog as well. Stay here in the forum or get a support contract if you have time constraints.


Sorry, if you are hurt.

I am not posting question on the blog I was making point against a claim by Gavin. I might be wrong.

_________________
Vinod K. Singh


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 14, 2005 6:21 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
For the record - the problem you are asking about here in this thread has nothing to do with the false Verstant claim you quoted.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 14, 2005 6:32 am 
Beginner
Beginner

Joined: Mon Dec 15, 2003 5:25 am
Posts: 48
Location: Delhi, India
christian wrote:
For the record - the problem you are asking about here in this thread has nothing to do with the false Verstant claim you quoted.

It has.

The Versant claim & Gavin's reply-
Quote:
7. Hibernate does not guarantee the order of loading attributes in your class, so your setter
methods cannot reference other attributes within your class. This is because the setter
methods are used at runtime when loading an object from the database and it is possible that
you are accessing a field that is not loaded yet.


This is not correct, Hibernate guarantees that attributes are populated (either via setters, or direct instance variable access) in the order they are listed in the mapping document.


Now look above at 3rd post of this thread <http://forum.hibernate.org/viewtopic.php?p=2233396#2233396>

In the mapping type field comes before definition but still type field is not populated by the time definition (an UserType) is being populated.

_________________
Vinod K. Singh


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 14, 2005 6:46 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You are working with annotations, not XML mapping files. This is alpha quality software. Have a look at the source and contribute a patch if you need this functionality and it is not provided.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 14, 2005 6:50 am 
Beginner
Beginner

Joined: Mon Dec 15, 2003 5:25 am
Posts: 48
Location: Delhi, India
christian wrote:
You are working with annotations, not XML mapping files. This is alpha quality software. Have a look at the source and contribute a patch if you need this functionality and it is not provided.


Right. I hope Hibernate will work in same fashion with XML and annotations, if not now then when it is produciton quality software.

Thanks for the response.

_________________
Vinod K. Singh


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 14, 2005 7:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you are implementing a custom type. a custom type should not care about any other attributes on the object besides the value related directly to the columns stated in the mapping file.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 14, 2005 7:14 am 
Beginner
Beginner

Joined: Mon Dec 15, 2003 5:25 am
Posts: 48
Location: Delhi, India
max wrote:
you are implementing a custom type. a custom type should not care about any other attributes on the object besides the value related directly to the columns stated in the mapping file.


Max, my expection form Hibernate is that it should populate fields in the order they are defined irrespective of it's type.

_________________
Vinod K. Singh


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.