帮我看看这个hibernate的问题。

03-08-24 neuhawk
我是根据huihoo的教程来做的,可是更新的时候有点问题。

代码如下:

company与person的关系是one-to-many 的关系。

company:

public class Company implements Serializable {

/** identifier field */

private String id;

/** nullable persistent field */

private String name;

private Set employees;

/** full constructor */

public Company(java.lang.String name) {

this.name = name;

}

/** default constructor */

public Company() {

}

public java.lang.String getId() {

return this.id;

}

public void setId(java.lang.String id) {

this.id = id;

}

public java.lang.String getName() {

return this.name;

}

public void setName(java.lang.String name) {

this.name = name;

}

public void setEmployees(Set value) {

this.employees=value;

}

public Set getEmployees() {

return employees;

}

public void addEmployee(hh.Person value) {

employees.add(value);

}

public void removeEmployee(hh.Person value) {

employees.remove(value);

}

public void clearEmployees() {

employees.clear();

}

public String toString() {

return new ToStringBuilder(this)

.append("id", getId())

.toString();

}

public boolean equals(Object other) {

if ( !(other instanceof Company) ) return false;

Company castOther = (Company) other;

return new EqualsBuilder()

.append(this.getId(), castOther.getId())

.isEquals();

}

public int hashCode() {

return new HashCodeBuilder()

.append(getId())

.toHashCode();

}

}

///////////////////////////////////////////////

person代码:

public class Person implements Serializable {

/** identifier field */

private String id;

/** nullable persistent field */

private String name;

/** nullable persistent field */

private String address;

/** nullable persistent field */

private String company_id;

/** full constructor */

public Person(java.lang.String name, java.lang.String address, java.lang.String company_id) {

this.name = name;

this.address = address;

this.company_id = company_id;

}

/** default constructor */

public Person() {

}

public java.lang.String getId() {

return this.id;

}

public void setId(java.lang.String id) {

this.id = id;

}

public java.lang.String getName() {

return this.name;

}

public void setName(java.lang.String name) {

this.name = name;

}

public java.lang.String getAddress() {

return this.address;

}

public void setAddress(java.lang.String address) {

this.address = address;

}

public java.lang.String getCompany_id() {

return this.company_id;

}

public void setCompany_id(java.lang.String companyId) {

this.company_id = companyId;

}

public String toString() {

return new ToStringBuilder(this)

.append("id", getId())

.toString();

}

public boolean equals(Object other) {

if ( !(other instanceof Person) ) return false;

Person castOther = (Person) other;

return new EqualsBuilder()

.append(this.getId(), castOther.getId())

.isEquals();

}

public int hashCode() {

return new HashCodeBuilder()

.append(getId())

.toHashCode();

}

}

////////////////////////////////

Company.hbm.xml:

<?xml version="1.0" encoding="GB2312"?>

<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>

<class name="hh.Company">

<!--hibernate为我们生成主键id-->

<id name = "id" unsaved-value = "null">

<generator class="uuid.hex"/>

</id>

<property name="name"/>

<!--1:n关系的映射-->

<set name="employees" cascade="all" inverse="true" lazy="true">

<key column="company_id"/>

<one-to-many class="hh.Person"/>

</set>

</class>

</hibernate-mapping>

//////////////////////////////////

Person.hbm.xml

<?xml version="1.0" encoding="GB2312"?>

<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>

<class name="hh.Person">

<!--hibernate为我们生成主键id-->

<id name = "id" unsaved-value = "null">

<generator class="uuid.hex"/>

</id>

<!--默认把类的变量映射为相同名字的表列,当然我们可以修改其映射方式-->

<property name="name"/>

<property name="address"/>

<property name="company_id"/>

</class>

</hibernate-mapping>

///////////////////////////////////////////

test代码:

public class TestCompany {

public TestCompany() {

}

private static SessionFactory sessions;

public static void main(String[] args) throws Exception{

//配置环境,分析xml映射文件

Configuration conf= new Configuration()

.addClass(Person.class)

.addClass(Company.class);

//生成并输出sql到文件(当前目录)和数据库

//SchemaExport dbExport=new SchemaExport(conf);

// dbExport.setOutputFile("build\\sql.txt");

// dbExport.create(true, true);

sessions = conf.buildSessionFactory();

//以上都是些固定格式的环境配置

//start......

Session s = sessions.openSession();

Transaction t = s.beginTransaction();

Set myset=new HashSet();

//1.用普通方式建立对象,填充数据

Company c=new Company();

c.setName("东软");

Person p1=new Person();

p1.setName("陈鹏");

p1.setAddress("西安东郊黄陵");

Person p2=new Person();

p2.setName("孙昱鹏");

p2.setAddress("南郊电子城");

//c.addEmployee(p1);

// c.addEmployee(p2);

myset.clear();

myset.add(p1);

myset.add(p2);

if (myset.contains(p2)&&myset.contains(p1))

{

System.out.println("#################################");

}

//2.持久化

c.setEmployees(myset);

s.saveOrUpdate(c);

s.flush();

//s.save(c);

//此时c,p1,p2已经可以在数据库中找到

t.commit();

s.close();

}

}

//////////////////////////////////////////

/////////////////////////////////////////////

问题:

1,数据库里只插入了p1的值,并没有插入p2的值。

2,person里的company_id值为null.

谢谢!

neuhawk
2003-08-24 22:19
奇怪,xml问件怎么没有copy出来。

Company.hbm.xml:

<?xml version="1.0" encoding="GB2312"?>

<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>

<class name="hh.Company">

<!--hibernate为我们生成主键id-->

<id name = "id" unsaved-value = "null">

<generator class="uuid.hex"/>

</id>

<property name="name"/>

<!--1:n关系的映射-->

<set name="employees" cascade="all" inverse="true" lazy="true">

<key column="company_id"/>

<one-to-many class="hh.Person"/>

</set>

</class>

</hibernate-mapping>

////////////////////////

Person.hbm.xml:

<?xml version="1.0" encoding="GB2312"?>

<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>

<class name="hh.Person">

<!--hibernate为我们生成主键id-->

<id name = "id" unsaved-value = "null">

<generator class="uuid.hex"/>

</id>

<!--默认把类的变量映射为相同名字的表列,当然我们可以修改其映射方式-->

<property name="name"/>

<property name="address"/>

<property name="company_id"/>

</class>

</hibernate-mapping>

neuhawk
2003-08-24 22:27
第二个问题已经解决,把inverse="false"就行。

但是第一个问题还没有出来,555

yehs220
2003-08-25 08:43
还是看不到映射文件。

neuhawk
2003-08-25 11:42
如何才能在这里显示XML文件啊。

猜你喜欢
2Go 1 2 下一页