-->
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: @Formula and enum values
PostPosted: Wed Feb 17, 2010 1:05 pm 
Newbie

Joined: Wed Feb 17, 2010 12:29 pm
Posts: 3
Hello all, I need help because I want to do something and I don't know why, let me explain:

I have an internal database procedure for calculating some stuff, and I want to retrieve the result in my application. This is done with the @Formula annotation:

@Formula("(select my_function(param1, param2))")
Integer myValue;

This is working great. The problem is that we are trying to do something a little bit more complicated. This function will always return us a value between two ends, and we defined already in our java project an Enum type that represents all the possible values, in this way:

public enum MyEnum {

VALUE1(1),

VALUE2(2),

private int value;

/**
* Constructor.
* @param value Literal value
*/
private MyEnum(int value) {
this.value = value;
}

/**
* Gets the literal value.
* @return Literal value
*/
public int toValue() {
return value;
}

/**
* Gets a literal instance by value.
* @param value Literal value
* @return The literal with the given value. Returns <tt>null</tt> when the value does not belong to a defined literal.
*/
public static MyEnum fromValue(int value) {
switch (value) {
case 1:
return VALUE1;
case 2:
return VALUE2;
default:
return null;
}
}

}

The idea is to change the field from Integer to MyEnum and teach hibernate how to set the value. I tried to use the @Type annotation together with the @Formula annotation, but this seems not to be a good idea since I'm getting this exception.

java.lang.NullPointerException
at org.hibernate.cfg.Ejb3Column.getName(Ejb3Column.java:203)
at org.hibernate.cfg.annotations.SimpleValueBinder.setType(SimpleValueBinder.java:172)
at org.hibernate.cfg.annotations.PropertyBinder.bind(PropertyBinder.java:120)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1667)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1148)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1226)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:173)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:854)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:425)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:131)
at org.jboss.jpa.deployment.PersistenceUnitDeployment.start(PersistenceUnitDeployment.java:301)

Does anyone knows how to do it?

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: @Formula and enum values
PostPosted: Wed Feb 17, 2010 8:16 pm 
Newbie

Joined: Sun Feb 14, 2010 8:08 am
Posts: 12
I hope this might help u https://www.hibernate.org/272.html


Top
 Profile  
 
 Post subject: Re: @Formula and enum values
PostPosted: Thu Feb 18, 2010 8:54 am 
Newbie

Joined: Wed Feb 17, 2010 12:29 pm
Posts: 3
Exactly!, this is what I'm trying, but in my case the field is calculated, it's not stored, so I'm using the @Formula annotation instead the @Column annotation, and when I combine them in this way:

@Formula("(my_sql_statement)")
@Type(type="MyEnum")
private MyEnum myField = null;

I got this exception when I try to deploy

java.lang.NullPointerException
at org.hibernate.cfg.Ejb3Column.getName(Ejb3Column.java:203)
at org.hibernate.cfg.annotations.SimpleValueBinder.setType(SimpleValueBinder.java:172)
at org.hibernate.cfg.annotations.PropertyBinder.bind(PropertyBinder.java:120)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1667)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1148)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1226)
at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:173)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:854)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:425)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:131)
at org.jboss.jpa.deployment.PersistenceUnitDeployment.start(PersistenceUnitDeployment.java:301)


Top
 Profile  
 
 Post subject: Re: @Formula and enum values
PostPosted: Thu Feb 25, 2010 5:45 am 
Newbie

Joined: Wed Feb 17, 2010 12:29 pm
Posts: 3
anyone?


Top
 Profile  
 
 Post subject: Re: @Formula and enum values
PostPosted: Wed Jun 22, 2011 5:04 am 
Newbie

Joined: Wed Mar 10, 2010 10:14 am
Posts: 12
Hi, we have exactly the same problem. I think the problem is in the org.hibernate.cfg.annotations.SimpleValueBinder.setType(XProperty, XClass) method. I have created a bug http://opensource.atlassian.com/projects/hibernate/browse/HHH-6347

One possible workaround is:

@Formula("(my_sql_statement)")
@Type(type="MyEnum")
private Object MyEnum = null;

public MyEnum getMyField() { return (MyEnum)this.myField; }


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.