我有一个定义为Formula
的公式:
@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
}
但是它正在忽略它。
这是我的“持久化单元”:
<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>
SQL 语句生成时不包含 Formula
。
如果我去掉 @Transient
,JPA
就会尝试加载 state
列,然后失败。
我想根据另一个实体的状态来计算状态。这就是我认为需要使用 Formula
的原因。
谢谢!
Udo。
@Formula
中,或许最好在select
子句周围加上括号? - WesternGun