【问题标题】:Hibernate org.hibernate.MappingException for non-annotated field未注释字段的休眠 org.hibernate.MappingException
【发布时间】:2013-07-15 15:05:36
【问题描述】:

这是我的 POJO,一个简单的学生类。

@Proxy(lazy = false)
@Entity(name = "Students")
public class Student implements Serializable {

    private static final long serialVersionUID = -9182600037012718128L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @Column
    private String name;

    private List<Homework> homework; // <-- the problematic line

    public Student(){
    }

    public getId(){return id;}
    public setId(long id){this.id = id;}

    public getName(){return name;}
    public setName(String name){this.name = name;}

    public getHomework(){return homework;}
    public setHomework(List<Homework> homework){this.homework = homework;}
}

不幸的是,即使 homework 字段没有注释(因为我目前不想将其映射到我的数据库),我在运行我的应用程序时遇到了这个异常:

org.hibernate.MappingException: Could not determine type for: java.util.List, at table: Students, for columns: [org.hibernate.mapping.Column(homework)]

这是我的 hibernate-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
    <property name="username" value="root" />
    <property name="password" value="root" />
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
    <property name="testOnBorrow" value="true" />
    <property name="validationQuery" value="select 1" />
</bean>

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
            hibernate.hbm2ddl.auto=update
            hibernate.show.sql=true
        </value>
    </property>
    <property name="annotatedClasses">
        <list>
            <value>com.test.entity.Student</value>
        </list>
    </property>
</bean>

感谢任何帮助! 谢谢!

【问题讨论】:

    标签: java mysql database hibernate hibernate-mapping


    【解决方案1】:

    您可以将非映射字段设为transient,以使hibernate 不会尝试将其与DB 映射

    private transient List<Homework> homework; 
    

    或者你可以用@javax.persistence.Transient注解来注解

    @Transient
    private List<Homework> homework; 
    

    hibernate 的一个特点是,它尝试将 Entity 类的所有字段与表的相应列进行映射。所以对于变量homework,它会在映射表中搜索对应的同名列"homework"(不区分大小写)。

    查看文档here,上面写着

    每个非静态非瞬态属性(字段或方法取决于 实体的访问类型)被认为是持久的,除非您 将其注释为@Transient。没有您的财产的注释 相当于相应的@Basic 注解。

    【讨论】:

    • 感谢您的快速响应...但是为什么它仍然被映射? Hibernate 不应该只映射带注释的字段吗?
    猜你喜欢
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    相关资源
    最近更新 更多