Hibernate中一对一实体关系的主键关联问题,急
小弟在利用Hibernate进行一对一关系操作时,利用的主键关联.遇到了下面的问题,现将问题描述如下:
在MySQL的quickstart数据库中有两个表,t_student和t_card。
t_student的构建语句如下:
create table t_student(id int primary key,name varchar(20),card_id varchar(20),sex varchar(20),
age int));
t_card的结构如下:
create table t_card(id int primary key,name varchar(20));
通过Hibernate的一对一实体关系的主键关联来操作实体Student和Card
Student.java的代码如下:
package register;
import register.Card;
public class Student{
private int id;
private String name;
private String card_id;
private String sex;
private int age;
private Card card;
public Student(){
}
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getCard_id(){
return card_id;
}
public void setCard_id(String card_id){
this.card_id=card_id;
}
public String getSex(){
return sex;
}
public void setSex(String sex){
this.sex=sex;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
public Card getCard(){
return card;
}
public void setCard(Card card){
this.card=card;
}
}
Card.java的代码如下:
package register;
import register.Student;
public class Card{
private int id;
private String name;
private Student stu;
public Card(){
}
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public Student getStudent(){
return stu;
}
public void setStudent(Student stu){
this.stu=stu;
}
}
Hibernate的配置文件如下:
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN "
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
<hibernate-configuration>
<session-factory>
<property name= "connection.datasource "> java:comp/env/jdbc/quickstart </property>
<property name= "dialect "> or