-
hibernate用注解 一对多 报Could not determine type for错误5
配置文件
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<mapping class="tests.cn.com.taiji.sms.entiry.Book" />
<mapping class="tests.cn.com.taiji.sms.entiry.Student" />
</session-factory>
</hibernate-configuration>
Student.java
package tests.cn.com.taiji.sms.entiry;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import tests.cn.com.taiji.sms.entiry.Book;
@Entity
@Table(name="stuent")
public class Student
{
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String memo;
private String name;
private List<Book> book;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="meno")
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(mappedBy="student",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
public List<Book> getBook()
{
return book;
}
public void setBook(List<Book> book)
{
this.book = book;
}
}
BOOK.java
package tests.cn.com.taiji.sms.entiry;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="zqBook")
public class Book
{
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String title;
private Student student;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@ManyToOne(cascade=CascadeType.REFRESH,optional=false)
@JoinColumn(name="stu_id",referencedColumnName="id")
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
StudentBookManager
package tests.cn.com.taiji.mhsms.manager;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import tests.cn.com.taiji.mhsms.web.BasicSessionFactory;
import tests.cn.com.taiji.sms.entiry.Book;
import tests.cn.com.taiji.sms.entiry.Student;
public class StudentBookManager
{
public static void main(String[] args)
{
Student student=new Student();
Book book1=new Book();
Book book2=new Book();
student.setName("王11");
List<Book> listBook=new ArrayList<Book>();
book1.setTitle("英语");
book2.setTitle("语文");
book1.setStudent(student);
book2.setStudent(student);
listBook.add(book1);
listBook.add(book2);
student.setBook(listBook);
Session session=null;
Transaction tx=null;
try
{
SessionFactory sessionFactory=BasicSessionFactory.sessionFactory;
session=sessionFactory.openSession();
tx=session.beginTransaction();
tx.begin();
session.save(student);
tx.commit();
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
//session.close();
}
}
}
公共类:
BasicSessionFactory
package tests.cn.com.taiji.mhsms.web;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class BasicSessionFactory
{
public static SessionFactory sessionFactory;
static
{
sessionFactory=new AnnotationConfiguration().configure("/hibernate/hibernate.cfg.xml").buildSessionFactory();///注意是AnnotationConfiguration,而不是Configuration
}
}
--------------------------------
在运行StudentBookManager时,出现
Exception in thread "main" java.lang.ExceptionInInitializerError
at tests.cn.com.taiji.mhsms.manager.StudentBookManager.main(StudentBookManager.java:39)
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: stuent, for columns: [org.hibernate.mapping.Column(book)]
自己试了试将,将标注写到字段上就不会出现这些问题!
我们现在做的工程,就完全可以写在get方法上,请各位高人给指点一下为啥不能写在get方法上呢?自己从网上搜了一下,说必须要么全部写在字段上,要么全部写在get 方法上,但我的这个都是写在了get方法上,为什么还是不行呢?
解决方案:
自己试了近两天,终于将问题找出来了,在一个实体文件中,注解要么全部放在字段上,要么全部放在get方法上,不能混合使用!2010年8月26日 15:32
目前还没有答案
相关推荐
Android Studio更新3.6.3之后出现Could not resolve all artifacts for configuration ‘:classpath’.问题你是否和我一样,只因在人群中多看了他一眼?![在这里插入图片描述]...
java的环境变量,关于JDK的配置,对于学习java的人有较好的帮助
Determine Drive Type
windows下的php-8.0.3,线程安全,64位操作系统。
FlippableStackView.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Oracle Application Express Installation Guide For Oracle Database 12
安卓手机在查看数据库,网络请求,sp等文件时,手机需要root,stetho可以直接在chrome里面查看到这些东西,不需要root,这里是相关的一些jar文件,针对于eclipse使用者
// Probably need to make sure we are not allocating objects (strings, etc.) in loops public class MyGallery extends FrameLayout { // Constants private final int swipe_min_distance = 120; private...
错误日志如下: 5-31 12:47:40.088 23896-23896/? E/JIGUANG-JCore: [JCoreGlobal] Get sdk version fail![获取sdk版本失败!] 05-31 12:47:40.088 23896-23896/? W/System.err: java.lang.UnsatisfiedLinkError: ...
How to Determine the Type of Supervisor Module
Determine if a File Exists
Wavelet analysis with its unique ... Experiment results show the method is very effective for sensor fault diagnosis, because it can not only detect the sensor faults, but also determine the fault type
vision_opencv-noetic.zip
判断系统是硬盘、移动磁盘、还是光驱,哈可以判断操作系统是32位还是64位的系统
Determine if a Directory Exists
Unable to determine default Pin/Port configuration! CCS 3.3烦人的提示,最终也没找到原因,太烦了,所以用APIHOOK,把它搞掉了 附带源程序 有需要的联系gongxd $ 126.com,
RFIDIOt_NFC_确定ISO标记类型_determine ISO tag type_python标准代码
dilanramirez.github.io
GUIDELINES TO DETERMINE IF A DRAWING NEEDS TO BE UPDATED WHEN GD&T IS NOT AFFECTED BY ENGINEERING CHANGE
could precisely determine the right prioritized feature set up front and break those features down into small product backlog items for the team. I had already delegated my engineering ...