簡體   English   中英

未注釋字段的Hibernate org.hibernate.MappingException

[英]Hibernate org.hibernate.MappingException for non-annotated field

這是我的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>

任何幫助表示贊賞! 謝謝!

您可以將非映射字段設置為transient ,以使休眠狀態不嘗試使用DB映射它

private transient List<Homework> homework; 

或者您可以使用@javax.persistence.Transient批注對其進行批注

@Transient
private List<Homework> homework; 

休眠的一項功能是,它嘗試將Entity類的所有字段與表的相應列進行映射。 因此,對於變量homework ,它將在映射表中搜索具有相同名稱“ homework” (不區分大小寫)的列。

看到這里的文檔,它說

實體的每個非靜態非瞬態屬性(取決於訪問類型的字段或方法)都被視為持久屬性,除非您將其注釋為@Transient。 沒有為您的屬性添加注釋等同於相應的@Basic注釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM