Hello_Bean.hbm.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping> <class name="hibernate.Hello_Bean" table="test_hibernate" >
<id name="id" column="id">
<generator class="identity"/>
</id>
<property name="name" type="string" update="true" insert="true" column="name" />
<property name="address" type="string" update="true" insert="true" column="address" />
<set name="userinfo_Beans" table="userinfo" inverse="false" cascade="all">
<key column="id"/>
<one-to-many class="hibernate.Userinfo_Bean"/>
</set>
</class>
</hibernate-mapping>
Userinfo_Bean.hbm.xml如下:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping> <class name="hibernate.Userinfo_Bean" table="userinfo" > <id name="uid" column="uid">
<generator class="identity"/>
</id> <property name="uname" type="string" update="true" insert="true" column="uname" />
<property name="passwd" type="string" update="true" insert="true" column="passwd" />
<many-to-one name="hello_Bean" column="id" class="hibernate.Hello_Bean"/>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping> <class name="hibernate.Hello_Bean" table="test_hibernate" >
<id name="id" column="id">
<generator class="identity"/>
</id>
<property name="name" type="string" update="true" insert="true" column="name" />
<property name="address" type="string" update="true" insert="true" column="address" />
<set name="userinfo_Beans" table="userinfo" inverse="false" cascade="all">
<key column="id"/>
<one-to-many class="hibernate.Userinfo_Bean"/>
</set>
</class>
</hibernate-mapping>
Userinfo_Bean.hbm.xml如下:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping> <class name="hibernate.Userinfo_Bean" table="userinfo" > <id name="uid" column="uid">
<generator class="identity"/>
</id> <property name="uname" type="string" update="true" insert="true" column="uname" />
<property name="passwd" type="string" update="true" insert="true" column="passwd" />
<many-to-one name="hello_Bean" column="id" class="hibernate.Hello_Bean"/>
</class>
</hibernate-mapping>
<set name="userinfo_Beans" table="userinfo" inverse="false" cascade="all">
<key column="id"/> //这个应该是userinfo_Beans的相关属性,uid
<one-to-many class="hibernate.Userinfo_Bean"/>
</set>
CREATE TABLE test_hibernate (
id int(3) unsigned NOT NULL auto_increment,
name varchar(20) default '0',
address varchar(20) default '0',
PRIMARY KEY (id)
)CREATE TABLE userinfo (
uid int(3) NOT NULL auto_increment,
uname varchar(20) default '0',
passwd varchar(20) default '0',
id int(3) unsigned default '0',
PRIMARY KEY (uid)
)
他们是通过id进行(test_hibernate )一对多(userinfo)的关联
2、它们的bean类如下:
(1)、hibernate.Hello_Bean
package hibernate;import java.io.Serializable;
import java.util.*;
public class Hello_Bean implements Serializable { private String name;
private String address;
private int id;
private Set userinfo_Beans=new HashSet();
public Hello_Bean() {} public Hello_Bean(String name, String address) {
this.name = name;
this.address = address;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public int getId() {
return id;
} public void setId(int id)
{
this.id = id;
}
public void setUserinfo_Beans(Set userinfo_Beans){
this.userinfo_Beans=userinfo_Beans;
}
public Set getUserinfo_Beans(){
return userinfo_Beans;
}
}
(2)、hibernate.Userinfo_Bean
package hibernate; import java.io.Serializable;
public class Userinfo_Bean implements Serializable {
private int uid;
private String uname;
private String passwd;
private Hello_Bean hello_Bean;
public Userinfo_Bean() {
}
public Userinfo_Bean(String uname,String passwd) {
this.uname=uname;
this.passwd=passwd;
}
public int getUid(){
return uid;
}
public void setUid(int uid){
this.uid=uid;
}
public String getUname(){
return uname;
}
public void setUname(String uname){
this.uname=uname;
}
public String getPasswd(){
return passwd;
}
public void setPasswd(String passwd){
this.passwd=passwd;
}
public Hello_Bean getHello_Bean(){
return hello_Bean;
}
public void setHello_Bean(Hello_Bean hello_Bean){
this.hello_Bean=hello_Bean;
}
}
你的xml文件和class是放到一个目录下吗?
如你的类是在hibernate下,xml文件也应该放在hibernate下
还有你的配置里:
<set name="userinfo_Beans" table="userinfo" inverse="false" cascade="all">
按参考的建议,这个inverse应该是等于"true"才对啊。many-to-one一端用默认的就可以了,但是一般会定义not-null="true"。
<property name="hibernate.hbm2ddl.auto">create</property>试一下
<html>
<body>
<font color="red">关注中</font>
</body>
</html>
把Hiberanate.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="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="hibernate/Room.hbm.xml"/>
<mapping resource="hibernate/User.hbm.xml"/>
<mapping resource="hibernate/Hello_Bean.hbm.xml"/>
<mapping resource="hibernate/Userinfo_Bean.hbm.xml"/>
<class-cache
class="hibernate.Room"
region="Room"
usage="read-write"/>
<class-cache
class="hibernate.User"
region="User"
usage="read-write"/>
<class-cache
class="hibernate.Hello_Bean"
region="Hello_Bean"
usage="read-write"/>
<class-cache
class="hibernate.Userinfo_Bean"
region="Userinfo_Bean"
usage="read-write"/>
</session-factory>
</hibernate-configuration>
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">有问题.如果你用的是hibernate2的话,就不应该用3.0的dtd
改成
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
能贴出代码看看吗?