【问题标题】:Why is Hibernate attempting to map a @Transient method?为什么 Hibernate 试图映射 @Transient 方法?
【发布时间】:2017-09-16 00:03:50
【问题描述】:

我有一个带注释的实体 POJO,我正在尝试与 Hibernate 一起使用。

该类有许多名称如getX 的业务逻辑方法。我已经用@Transient 注释对这些进行了注释,示例源如下:

import java.beans.Transient;
import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class TestEntity {

    private String id;

    @Id
    public String getId()
    {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Transient
    public Collection<String> getAllGames()
    {
        return null;
    }
}

然而,Hibernate 似乎仍在尝试映射这些方法。初始化Hibernate时输出如下错误:

org.hibernate.MappingException:无法确定类型: java.util.Collection,在表:TestEntity,列: [org.hibernate.mapping.Column(allGames)] 在 org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:455) 在 org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:422) 在 org.hibernate.mapping.Property.isValid(Property.java:226) 在 org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:597) 在 org.hibernate.mapping.RootClass.validate(RootClass.java:265) 在 org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329) 在 org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:451) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:710) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726) ...

使用&lt;mapping class="..."&gt; 条目配置休眠:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
       <property name="hibernate.dialect">
          org.hibernate.dialect.DerbyTenSevenDialect
       </property>
       <property name="hibernate.connection.driver_class">
          org.apache.derby.jdbc.EmbeddedDriver
       </property>

       <property name="hibernate.connection.url">
          jdbc:derby:db
       </property>

       <!-- Disable the second-level cache  -->
       <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

       <property name="hibernate.hbm2ddl.auto">update</property>
       <property name="show_sql">true</property>

       <mapping class="TestEntity"/>

    </session-factory>
</hibernate-configuration>

我正在运行 Hibernate 5.2.10.Final。

为什么 Hibernate 会尝试映射这种瞬态方法?

【问题讨论】:

  • 这道题还有什么需要重开的?

标签: java hibernate jpa


【解决方案1】:

@Transient 注释属于错误的包:

import java.beans.Transient;

它们应该来自javax.persistence 包:

import javax.persistence.Transient;

另请参阅:Do javax.persistence.transient and java.bean.transient do the same thing?

【讨论】:

    猜你喜欢
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 2014-02-23
    • 2016-05-12
    • 2012-03-07
    相关资源
    最近更新 更多