日期:2014-05-20  浏览次数:20752 次

Hibernate一对多关联,能查询,但不能联结插入
ava程序:
Account.java
package test;

import java.util.Set;

public class Account {
int aid;

String name = null;

String password = null;

Set email_Adr;

/**
* @return Returns the name.
*/
public String getName() {
return name;
}

/**
* @param name
* The name to set.
*/
public void setName(String name) {
this.name = name;
}

/**
* @return Returns the password.
*/
public String getPassword() {
return password;
}

/**
* @param password
* The password to set.
*/
public void setPassword(String password) {
this.password = password;
}

public int getAid() {
return aid;
}

public void setAid(int id) {
this.aid = id;
}

public Set getEmail_Adr() {
return email_Adr;
}

public void setEmail_Adr(Set email) {
this.email_Adr = email;
}
}
Email_Adr.java
package test;

import java.util.HashSet;
import java.util.Set;



public class Email_Adr {
int eid;
String emailAdrr;
Account account;
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public int getEid() {
return eid;
}
public void setEid(int id) {
this.eid = id;
}
public String getEmailAdrr() {
return emailAdrr;
}
public void setEmailAdrr(String emailAdrr) {
this.emailAdrr = emailAdrr;
}

}
数据库描述:
CREATE TABLE `account` (
  `aid` int(6) unsigned NOT NULL auto_increment,
  `name` char(10) default NULL,
  `password` char(10) default NULL,
  PRIMARY KEY (`aid`)
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=gbk;

CREATE TABLE `email_adr` (
  `eid` int(6) unsigned NOT NULL auto_increment,
  `emailAdrr` varchar(20) default NULL,
  `aaid` tinyint(4) default NULL,
  PRIMARY KEY (`eid`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=gbk;

配置文件:
Account.hbm.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping
  PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

  <class name="test.Account" table="account">
  <id name="aid" column="aid">
  <generator class="assigned" />
  </id>
  <property name="name" />
  <property name="password" />

  <set name="Email_Adr" table="email_adr" lazy="true"
  cascade="all" inverse="true">
  <key column="aaid" ></key>
  <one-to-many class="test.Email_Adr" />
  </set>

  </class>




</hibernate-mapping>

Email:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
  PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

  <class name="test.Email_Adr" table="email_adr">
  <id name="eid" column="eid">
  <generator class="assigned" />
  </id>
<many-to-one name="Account" column="aaid" class="test.Account" cascade="all" ></many-to-one>