Hibernate with JPA ignoring @Formula
Solution 1
You're trying to map the state twice : once by adding annotations to the field, and once by adding annotations to the getter. Put all the annotations at the same place (and at the same place as the @Id
annotation).
But this is really confusing. Your state is transient (meaning it's not mapped at all, and should not be read from the database), but it's also enumerated (why would Hibernate use this annotation since it's supposed to be transient), and a formula.
Finally, a formula is some piece of SQL that's added in every SQL that is used to load your entity. So it should not contain a select clause, or a from clause, but just a formula using some of the columns of the table itself. For example, suppose you have a table with a column salary
and a column bonus
, you could have a formula for a field totalIncome
which would be 'bonus + salary'. But id doesn't go much further than that.
Solution 2
I think this should work. I think @Transient is making Hibernate ignore the attribute at all.
@Entity
@Table(name = "MyEntity")
@org.hibernate.annotations.Table(appliesTo = "MyEntity")
public class MyEntity
{
// MAYBE YOU HAVE TO MOVE IT TO THE GETTER @Enumerated(value = javax.persistence.EnumType.STRING)
// REMOVE THIS @Transient
private State state;
@Enumerated(value = javax.persistence.EnumType.STRING) // MOVED
@Formula(value = "(select e.state from OtheEntity e)")
public State getState()
{
return this.state;
}
//setter and another properties
}
Comments
-
ssedano almost 2 years
I have a
Formula
defined as:@Entity @Table(name = "MyEntity") @org.hibernate.annotations.Table(appliesTo = "MyEntity") public class MyEntity { @Enumerated(value = javax.persistence.EnumType.STRING) @Transient @Formula(value = "select e.state from OTHER_ENTITY e") private State state; public State getState() { return this.state; } //setter and another properties }
But it is ignoring it.
Here is my
Persistence Unit
:<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="myPersistence" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <mapping-file>META-INF/orm.xml</mapping-file> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /> <property name="hibernate.connection.url" value="jdbc:mysql://url:3306/db" /> <property name="hibernate.connection.username" value="root" /> <property name="hibernate.connection.password" value="root" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.hbm2ddl.auto" value="validate" /> </properties> </persistence-unit>
The sql are generated without the
Formula
.If I remove the
@Transient
theJPA
tries to load thestate
column and then fails.I want to calculate the state based on the state of another entity. That's why the I think a
Formula
works.Thank you!
Udo.