This is my POJO, a simple student class.
这是我的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;}
}
Unfortunately, even though the homework
field is not annotated (since I currently do not want to map it to my DB), I get this exception when running my application:
不幸的是,即使作业字段没有注释(因为我目前不想将它映射到我的数据库),我在运行我的应用程序时遇到此异常:
org.hibernate.MappingException: Could not determine type for: java.util.List, at table: Students, for columns: [org.hibernate.mapping.Column(homework)]
This is my hibernate-config.xml:
这是我的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>
Any help is appreciated! Thanks!
任何帮助表示赞赏!谢谢!
1 个解决方案
#1
5
You can make non mapped field as transient
, to make hibernate not try to map it with DB
您可以将非映射字段设置为瞬态,以使休眠不尝试将其映射到DB
private transient List<Homework> homework;
or you can annotate it with @javax.persistence.Transient
annotation
或者您可以使用@ javax.persistence.Transient注释对其进行注释
@Transient
private List<Homework> homework;
One feature of hibernate is, it tries to map all the fields of Entity class with the corresponding columns of the table. So for the variable homework
, it searches for the corresponding column with the same name "homework" (case -insensitive) in the mapped table.
hibernate的一个特性是,它试图将Entity类的所有字段映射到表的相应列。因此,对于变量作业,它在映射表中搜索具有相同名称“homework”(case -sensitive)的相应列。
See the documentation here, and it says
请参阅此处的文档,并说明
Every non static non transient property (field or method depending on the access type) of an entity is considered persistent, unless you annotate it as @Transient. Not having an annotation for your property is equivalent to the appropriate @Basic annotation.
除非您将实体注释为@Transient,否则实体的每个非静态非瞬态属性(字段或方法取决于访问类型)都被视为持久性。没有您的属性的注释相当于相应的@Basic注释。
#1
5
You can make non mapped field as transient
, to make hibernate not try to map it with DB
您可以将非映射字段设置为瞬态,以使休眠不尝试将其映射到DB
private transient List<Homework> homework;
or you can annotate it with @javax.persistence.Transient
annotation
或者您可以使用@ javax.persistence.Transient注释对其进行注释
@Transient
private List<Homework> homework;
One feature of hibernate is, it tries to map all the fields of Entity class with the corresponding columns of the table. So for the variable homework
, it searches for the corresponding column with the same name "homework" (case -insensitive) in the mapped table.
hibernate的一个特性是,它试图将Entity类的所有字段映射到表的相应列。因此,对于变量作业,它在映射表中搜索具有相同名称“homework”(case -sensitive)的相应列。
See the documentation here, and it says
请参阅此处的文档,并说明
Every non static non transient property (field or method depending on the access type) of an entity is considered persistent, unless you annotate it as @Transient. Not having an annotation for your property is equivalent to the appropriate @Basic annotation.
除非您将实体注释为@Transient,否则实体的每个非静态非瞬态属性(字段或方法取决于访问类型)都被视为持久性。没有您的属性的注释相当于相应的@Basic注释。