在本文中主要實現數據的級聯插入
下面是User 和 Order兩個pojo類的代碼
[java]
public class Order {
private int id;
private int whoid; // thi id of the order
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this
}
public int getId() {
return id;
}
public void setId(int id) {
this
}
public int getWhoid() {
return whoid;
}
public void setWhoid(int whoid) {
this
}
}
public class Order {
private int id;
private int whoid; // thi id of the order
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this
}
public int getId() {
return id;
}
public void setId(int id) {
this
}
public int getWhoid() {
return whoid;
}
public void setWhoid(int whoid) {
this
}
}
[java]
public class User {
private int id;
private String username;
private String password;
//這裡需要注意的是使用集合必須使用Set
private Set<Order> orders;
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
this
}
public int getId() {
return id;
}
public void setId(int id) {
this
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this
}
}
public class User {
private int id;
private String username;
private String password;
//這裡需要注意的是使用集合必須使用Set
private Set<Order> orders;
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
this
}
public int getId() {
return id;
}
public void setId(int id) {
this
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this
}
}
下面這個是UserDao
[java]
import org
import org
import com
public class UserDao {
private Session session;
public UserDao(Session session )
{
this
}
public void savaUser(User user)
{
Transaction tx = session
try
{
session
mit();
}
catch(Exception ex)
{
ex
tx
}
}
}
import org
import org
import com
public class UserDao {
private Session session;
public UserDao(Session session )
{
this
}
public void savaUser(User user)
{
Transaction tx = session
try
{
session
mit();
}
catch(Exception ex)
{
ex
tx
}
}
}
下面的這兩個hbm
User
[html]
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<property name=
<set name=
<key column=
<one
</set>
</class>
</hibernate
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<property name=
<set name=
<key column=
<one
</set>
</class>
</hibernate
其中要理解的就是set這個標簽
key標簽用來表明
Order
[html]
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<many
</class>
</hibernate
<?xml version=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<many
</class>
</hibernate
其中的 many
下面是測試代碼
[java]
public class Test {
public static void main(String args[])
{
User user = new User();
user
user
HashSet<Order> orders = new HashSet<Order>();
Order o
Order o
Order o
orders
orders
orders
user
Session session = HibernateSessionFactory
UserDao dao = new UserDao(session);
dao
}
}
public class Test {
public static void main(String args[])
{
User user = new User();
user
user
HashSet<Order> orders = new HashSet<Order>();
Order o
Order o
Order o
orders
orders
orders
user
Session session = HibernateSessionFactory
UserDao dao = new UserDao(session);
dao
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28639.html