休眠与 JPA 忽略@Formula
我有一个 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 没有公式
。
如果我删除 @Transient
,JPA
会尝试加载 state
列,然后失败。
我想根据另一个实体的状态来计算状态。这就是为什么我认为公式
有效。
谢谢你!
乌多.
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
the JPA
tries to load the state
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尝试映射状态两次:一次通过向字段添加注释,一次通过向 getter 添加注释。将所有注释放在同一位置(与
@Id
注释位于同一位置)。但这确实令人困惑。你的状态是瞬态的(意味着它根本没有映射,并且不应该从数据库中读取),但它也是枚举的(为什么 Hibernate 要使用这个注释,因为它应该是瞬态的)和一个公式。
最后,公式是添加到用于加载实体的每个 SQL 中的某些 SQL 片段。因此它不应包含 select 子句或 from 子句,而应仅包含使用表本身的某些列的公式。例如,假设您有一个包含
salary
列和bonus
列的表,您可以为字段totalIncome
制定一个公式,该公式为“奖金+工资”。但 id 并没有比这更进一步。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 columnbonus
, you could have a formula for a fieldtotalIncome
which would be 'bonus + salary'. But id doesn't go much further than that.我认为这应该有效。我认为 @Transient 正在使 Hibernate 完全忽略该属性。
I think this should work. I think @Transient is making Hibernate ignore the attribute at all.