在练习Hibernate多对多出现了问题,但是怎么也发现不了问题的所在,很苦恼!希望高人指点错误的所在。
项目的结构是这样的
|---test(包)
| |--Course.java
| |--ExportDB.java
| |--Student.java
| |--Test.java
| |--Course.hbm.xml
| |--Student.hbm.xml
|------hibernate.cfg.xml
Java code
--------------------------
hibernate.cfg.xml文件的配置如下:<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:oracle</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="hibernate.myeclipse.connection.profile">oracle</property>
<property name="hibernate.connection.password">tiger</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping resource="test/Course.hbm.xml" />
<mapping resource="test/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>-------------------------
Student.javapackage test;
import java.io.Serializable;
import java.util.HashSet;public class Student implements Serializable{ private long id;
private String name;
private HashSet courses = new HashSet();
public Student(){}
public Student(String name,HashSet courses){
this.name = name;
this.courses = courses;
}
public HashSet getCourses() {
return courses;
}
public void setCourses(HashSet courses) {
this.courses = courses;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
************************
Student.hbm.xml
<hibernate-mapping>
<class name="test.Student" table="d_student">
<id name="id" type="java.lang.Long">
<column name="id"></column>
<generator class="sequence">
<param name="sequence">stu_seq</param>
</generator>
</id>
<property name="name" type="java.lang.String"></property>
<set name="courses" table="d_student_course" cascade="all">
<key column="studentid"></key>
<many-to-many class="test.Course" column="courseid" outer-join="true"></many-to-many>
</set>
</class>
</hibernate-mapping>
----------------------------------
Course.java
package test;import java.io.Serializable;
import java.util.HashSet;public class Course implements Serializable{ private long id;
private String name;
private HashSet students = new HashSet();
public Course(){}
public Course(String name,HashSet students){
this.name = name;
this.students = students;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HashSet getStudents() {
return students;
}
public void setStudents(HashSet students) {
this.students = students;
}
}
************************************
Course.hbm.xml<hibernate-mapping>
<class name="test.Course" table="d_course">
<id name="id" type="java.lang.Long">
<column name="id"></column>
<generator class="sequence">
<param name="sequence">cou_seq</param>
</generator>
</id>
<property name="name" type="java.lang.String" />
<set name="students" table="d_student_course" cascade="all" inverse="true">
<key column="courseid"></key>
<many-to-many class="test.Student" column="studentid" outer-join="true"></many-to-many>
</set>
</class>
</hibernate-mapping>
--------------------------------------------------
ExportDB.java
package test;import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;public class ExportDB {
public static void main(String[] args){
Configuration con = new Configuration().configure();
SchemaExport sche = new SchemaExport(con);
sche.create(true,true);
}
}
********************************************
运行ExportDB.java之后,可以看见控制台显示如下:
drop table d_course cascade constraints drop table d_student cascade constraints drop table d_student_course cascade constraints drop sequence cou_seq drop sequence stu_seq create table d_course (
id number(19,0) not null,
name varchar2(255 char),
primary key (id)
) create table d_student (
id number(19,0) not null,
name varchar2(255 char),
primary key (id)
) create table d_student_course (
courseid number(19,0) not null,
studentid number(19,0) not null,
primary key (studentid, courseid)
) alter table d_student_course
add constraint FK260A8BDA6972978D
foreign key (courseid)
references d_course alter table d_student_course
add constraint FK260A8BDA4ECDA955
foreign key (studentid)
references d_student create sequence cou_seq create sequence stu_seq***************************************************
Test.java
package test;import java.util.HashSet;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test { Configuration con;
SessionFactory factory;
Session session;
public Test(){
con = new Configuration().configure();
factory = con.buildSessionFactory();
}
public void insert(){
try{
session = factory.openSession();
Student stu1 = new Student();
Student stu2 = new Student();
HashSet set1 = new HashSet();
HashSet set2 = new HashSet();
Course cou1 = new Course();
cou1.setName("English");
Course cou2 = new Course();
cou2.setName("China");
set1.add(cou1);
set1.add(cou2);
set2.add(cou1);
set2.add(cou2);
stu1.setCourses(set1);
stu2.setCourses(set2);
session.save(stu1);
session.save(stu2);
session.beginTransaction().commit();
}catch(HibernateException hh){
hh.printStackTrace();
session.beginTransaction().rollback();
}finally{
if(session.isOpen()){
session.close();
}
}
}
public static void main(String[] args){
Test test = new Test();
test.insert();
}
}
但是再运行Test.java进行添加数据的时候却出现这个错误,我在网上Google了,但是还是无法查出错误的所在,迫切的希望前辈指点迷津!log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate:
select
stu_seq.nextval
from
dual
org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of test.Student.setCourses
---------------------------------------------
真诚感谢您的指点~
项目的结构是这样的
|---test(包)
| |--Course.java
| |--ExportDB.java
| |--Student.java
| |--Test.java
| |--Course.hbm.xml
| |--Student.hbm.xml
|------hibernate.cfg.xml
Java code
--------------------------
hibernate.cfg.xml文件的配置如下:<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:oracle</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="hibernate.myeclipse.connection.profile">oracle</property>
<property name="hibernate.connection.password">tiger</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping resource="test/Course.hbm.xml" />
<mapping resource="test/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>-------------------------
Student.javapackage test;
import java.io.Serializable;
import java.util.HashSet;public class Student implements Serializable{ private long id;
private String name;
private HashSet courses = new HashSet();
public Student(){}
public Student(String name,HashSet courses){
this.name = name;
this.courses = courses;
}
public HashSet getCourses() {
return courses;
}
public void setCourses(HashSet courses) {
this.courses = courses;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
************************
Student.hbm.xml
<hibernate-mapping>
<class name="test.Student" table="d_student">
<id name="id" type="java.lang.Long">
<column name="id"></column>
<generator class="sequence">
<param name="sequence">stu_seq</param>
</generator>
</id>
<property name="name" type="java.lang.String"></property>
<set name="courses" table="d_student_course" cascade="all">
<key column="studentid"></key>
<many-to-many class="test.Course" column="courseid" outer-join="true"></many-to-many>
</set>
</class>
</hibernate-mapping>
----------------------------------
Course.java
package test;import java.io.Serializable;
import java.util.HashSet;public class Course implements Serializable{ private long id;
private String name;
private HashSet students = new HashSet();
public Course(){}
public Course(String name,HashSet students){
this.name = name;
this.students = students;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HashSet getStudents() {
return students;
}
public void setStudents(HashSet students) {
this.students = students;
}
}
************************************
Course.hbm.xml<hibernate-mapping>
<class name="test.Course" table="d_course">
<id name="id" type="java.lang.Long">
<column name="id"></column>
<generator class="sequence">
<param name="sequence">cou_seq</param>
</generator>
</id>
<property name="name" type="java.lang.String" />
<set name="students" table="d_student_course" cascade="all" inverse="true">
<key column="courseid"></key>
<many-to-many class="test.Student" column="studentid" outer-join="true"></many-to-many>
</set>
</class>
</hibernate-mapping>
--------------------------------------------------
ExportDB.java
package test;import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;public class ExportDB {
public static void main(String[] args){
Configuration con = new Configuration().configure();
SchemaExport sche = new SchemaExport(con);
sche.create(true,true);
}
}
********************************************
运行ExportDB.java之后,可以看见控制台显示如下:
drop table d_course cascade constraints drop table d_student cascade constraints drop table d_student_course cascade constraints drop sequence cou_seq drop sequence stu_seq create table d_course (
id number(19,0) not null,
name varchar2(255 char),
primary key (id)
) create table d_student (
id number(19,0) not null,
name varchar2(255 char),
primary key (id)
) create table d_student_course (
courseid number(19,0) not null,
studentid number(19,0) not null,
primary key (studentid, courseid)
) alter table d_student_course
add constraint FK260A8BDA6972978D
foreign key (courseid)
references d_course alter table d_student_course
add constraint FK260A8BDA4ECDA955
foreign key (studentid)
references d_student create sequence cou_seq create sequence stu_seq***************************************************
Test.java
package test;import java.util.HashSet;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Test { Configuration con;
SessionFactory factory;
Session session;
public Test(){
con = new Configuration().configure();
factory = con.buildSessionFactory();
}
public void insert(){
try{
session = factory.openSession();
Student stu1 = new Student();
Student stu2 = new Student();
HashSet set1 = new HashSet();
HashSet set2 = new HashSet();
Course cou1 = new Course();
cou1.setName("English");
Course cou2 = new Course();
cou2.setName("China");
set1.add(cou1);
set1.add(cou2);
set2.add(cou1);
set2.add(cou2);
stu1.setCourses(set1);
stu2.setCourses(set2);
session.save(stu1);
session.save(stu2);
session.beginTransaction().commit();
}catch(HibernateException hh){
hh.printStackTrace();
session.beginTransaction().rollback();
}finally{
if(session.isOpen()){
session.close();
}
}
}
public static void main(String[] args){
Test test = new Test();
test.insert();
}
}
但是再运行Test.java进行添加数据的时候却出现这个错误,我在网上Google了,但是还是无法查出错误的所在,迫切的希望前辈指点迷津!log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate:
select
stu_seq.nextval
from
dual
org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of test.Student.setCourses
---------------------------------------------
真诚感谢您的指点~
解决方案 »
- hibernate annotation 不能调用存储过程?如何调用?
- 大家帮帮忙,这个flash是用什么java flash图表工具完成的?
- 如何用java实现简体中文和繁体中文之间的转换
- 求解功能的实现(附图),急 - -
- 有关struts中的logic:iterator标签的奇怪问题
- (100分)谁有做项目的整个过程的视频?
- script标签实现跨域访问,高手进来看看吧
- 我和朋友争论的一个关于J2EE访问数据库的问题,到底是我对还是他对,菜鸟求助!
- 哪位给个javamail邮件群发的例子啊
- 关于servlet调用ejb的问题
- weblogic.xml无法生效
- oracle hibernate 取一周内的数据 一个月内的数据hql 或者思路
<param name="sequence">stu_seq </param>
</generator>
Transaction tr=session.beginTransaction();
session.save(stu1);
session.save(stu2);
tr.commit();
Student stu1 = new Student();
Student stu2 = new Student();
HashSet set1 = new HashSet();
HashSet set2 = new HashSet();
Course cou1 = new Course();
cou1.setName("English");
Course cou2 = new Course();
cou2.setName("China");
set1.add(cou1);
set1.add(cou2);
set2.add(cou1);
set2.add(cou2);
stu1.setCourses(set1);
stu2.setCourses(set2);
1.Student 的name属性没有赋值。
2.Course 的students集合没有把实体Student添加进去程序改一下你试试
Student stu1 = new Student();
stu1.setName("tom");//对name赋值
Student stu2 = new Student();
stu2.setName("jerry");//对name赋值
HashSet set1 = new HashSet();
HashSet set2 = new HashSet(); HashSet setStu1 = new HashSet(); //cou1中用来存放Student的集合
HashSet setStu2 = new HashSet(); //cou2中用来存放Student的集合
Course cou1 = new Course();
cou1.setName("English");
Course cou2 = new Course();
cou2.setName("China");
set1.add(cou1);
setStu1.add(stu1);//学生stu1选了课,把stu1加到集合中
set1.add(cou2);
setStu2.add(stu1);//学生stu1选了课,把stu1加到集合中
set2.add(cou1);
setStu1.add(stu2);//学生stu2选了课,把stu2加到集合中
set2.add(cou2);
setStu2.add(stu2);//学生stu2选了课,把stu2加到集合中 stu1.setCourses(set1);
stu2.setCourses(set2); cou1.setSudents(setStu1);//注入选了cou1学生的集合
cou2.setSudents(setStu2);//注入选了cou2学生的集合
session.save(stu1);
session.save(stu2);
session.save(cou1); //保存课程cou1
session.save(cou2); //保存课程cou2加注释的是我加上去的。结合你原来的程序看看还有什么问题。