hibernate一对多双向关联问题,

一个TUser关联多个TAddress.
实体类:
TUser:
public class TUser implements java.io.Serializable {

// Fields
private Integer id;
private Integer age;
private String name;
private Set addresses = new HashSet();

// Constructors

/** default constructor */
public TUser() {
}


/** full constructor */
public TUser(Integer age, String name) {
this.age = age;
this.name = name;
}

// Property accessors

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public Integer getAge() {
return this.age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}
public Set getAddresses()
{
return addresses;
}

public void setAddresses(Set addresses)
{
this.addresses = addresses;
}
}
---------------------
TAddress:
public class TAddress implements java.io.Serializable {

// Fields
private Integer id;
private String address;
private String zipcode;
private TUser user;

// Constructors

/** default constructor */
public TAddress() {
}

/** full constructor */
public TAddress(String address, String zipcode, TUser user) {
this.address = address;
this.zipcode = zipcode;
this.user = user;
}


// Property accessors

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public String getAddress() {
return this.address;
}

public void setAddress(String address) {
this.address = address;
}

public String getZipcode() {
return this.zipcode;
}

public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}

public TUser getUser() {
return this.user;
}

public void setUser(TUser user) {
this.user = user;
}
}
------------------------------------------------
映射文件:
TUser.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.puma.entity.TUser" table="t_user" catalog="hibernatedb">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="age" type="java.lang.Integer">
<column name="age" />
</property>
<property name="name" type="java.lang.String">
<column name="name" length="50" />
</property>
<set name="addresses" table="t_address" cascade="all"
inverse="true">
<key column="userid" />
<one-to-many class="com.puma.entity.TAddress" />
</set>
</class>
</hibernate-mapping>
-------------------
TAddress.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.puma.entity.TAddress" table="t_address" catalog="hibernatedb">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="address" type="java.lang.String">
<column name="address" length="50" />
</property>
<property name="zipcode" type="java.lang.String">
<column name="zipcode" length="50" />
</property>
<many-to-one name="user" class="com.puma.entity.TUser"
fetch="select" outer-join="true">
<column name="userid" />
</many-to-one>
</class>
</hibernate-mapping>

---------------------------------------------------
public class One2ManyTest extends TestCase
{
Session session = null;

protected void setUp() throws Exception
{
try
{
Configuration configure = new Configuration().configure();
SessionFactory sessionFactory = configure.buildSessionFactory();
session = sessionFactory.openSession();
} catch (HibernateException exception)
{
exception.printStackTrace();
}
}

protected void tearDown() throws Exception
{
try
{
session.close();
} catch (HibernateException exception)
{
exception.printStackTrace();
}
}

public void testInsertUser()
{
Transaction trs = session.beginTransaction();
// 创建一个用户
TUser user = new TUser();
user.setName("huangyipingq");

//创建一个地址
TAddress addr = new TAddress();
addr.setAddress("hubei_jianshi");
addr.setZipcode("445300");

addr.setUser(user);

session.save(user);
session.save(addr);
trs.commit();
}

public void testGetById()
{
// TAddress address = (TAddress)session.get(TAddress.class, new Integer(10));

String hql = "from TAddress t where t.id=?";
TAddress address = (TAddress)session.createQuery(hql).setParameter(0, new Integer(9)).uniqueResult();
System.out.println(address.getZipcode());

}
}
此时数据库中有ID为10的这条记录:
mysql> select * from t_address;
+----+-----------------+---------+--------+
| id | address | zipcode | userid |
+----+-----------------+---------+--------+
| 4 | hubei_jianshi | 445300 | 9 |
| 7 | hubei_jianshi | 445300 | 9 |
| 8 | fdddddddddddddd | 343434 | 11 |
+----+-----------------+---------+--------+

下面testInsertUser()能够测试通过.但是下面这个方法一运行就抛出空指针异常.不知道怎么回事.
并且TAddress address = (TAddress)session.createQuery(hql).setParameter(0, new Integer(9)).uniqueResult();是这句代码抛出的.

注意TAddress是多的一方.而如果查询一的一方,也就是TUser则可以查出.
TUser address = (TUser)session.get(TUser.class, new Integer(2));
哪位帮小弟解决下,感激不尽

你可能感兴趣的:(Hibernate,mysql,xml,.net,MyEclipse)