为什么Hibernate试图映射@Transient方法?

时间:2017-09-16 00:03:51

标签: java hibernate jpa

我有一个带注释的实体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似乎仍试图映射这些方法。初始化休眠时输出以下错误:

  

org.hibernate.MappingException:无法确定以下类型:   java.util.Collection,在表:TestEntity,用于列:   [org.hibernate.mapping.Column(allGames)] at   org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:455)at at   org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:422)at at   org.hibernate.mapping.Property.isValid(Property.java:226)at   org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:597)     在org.hibernate.mapping.RootClass.validate(RootClass.java:265)at   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)   ...

使用<mapping class="...">条目配置Hibernate:

<?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会尝试映射这种瞬态方法?

1 个答案:

答案 0 :(得分:0)

@Transient注释包含错误:

import java.beans.Transient;

它们应来自javax.persistence包:

import javax.persistence.Transient;

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