Hibernate一对多双向关联

模型user,address

User中的userid和address中的user_id进行一对多

user sql

CREATE TABLE `NewTable` (
`userid`  int(11) NOT NULL AUTO_INCREMENT ,
`account`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`password`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
PRIMARY KEY (`userid`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=40
ROW_FORMAT=COMPACT
;

address sql

CREATE TABLE `NewTable` (
`addressid`  int(11) NOT NULL AUTO_INCREMENT ,
`addressinfo`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
`user_id`  int(11) NULL DEFAULT NULL ,
PRIMARY KEY (`addressid`),
FOREIGN KEY (`user_id`) REFERENCES `user` (`userid`) ON DELETE RESTRICT ON UPDATE RESTRICT,
INDEX `userud` (`user_id`) USING BTREE 
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=77
ROW_FORMAT=COMPACT
;

user

package dto;

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

public class User {

	private int userid;

	private String account;

	private String password;

	private Set<Address> addresses = new HashSet<Address>();

	public Set<Address> getAddresses() {
		return addresses;
	}

	public void setAddresses(Set<Address> addresses) {
		this.addresses = addresses;
	}

	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}

user.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2015-6-21 20:34:34 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="dto.User" table="user" catalog="snlu">
        <id name="userid" type="java.lang.Integer">
            <column name="userid" />
            <generator class="identity" />
        </id>
        <property name="account" type="string">
            <column name="account" />
        </property>
        <property name="password" type="string">
            <column name="password" />
        </property>
        <set name="addresses" table="address" inverse="false" lazy="true" fetch="select" cascade="save-update">
            <key>
                <column name="user_id" />
            </key>
            <one-to-many class="dto.Address" />
        </set>
    </class>
</hibernate-mapping>

address

package dto;

public class Address {
	private Integer addressid;
	private User user;
	private String addressinfo;

	public Integer getAddressid() {
		return addressid;
	}

	public void setAddressid(Integer addressid) {
		this.addressid = addressid;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getAddressinfo() {
		return addressinfo;
	}

	public void setAddressinfo(String addressinfo) {
		this.addressinfo = addressinfo;
	}

}

address.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2015-6-21 20:34:34 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="dto.Address" table="address" catalog="snlu">
        <id name="addressid" type="java.lang.Integer">
            <column name="addressid" />
            <generator class="identity" />
        </id>
        <many-to-one name="user" class="dto.User" fetch="select">
            <column name="user_id" />
        </many-to-one>
        <property name="addressinfo" type="string">
            <column name="addressinfo" />
        </property>
    </class>
</hibernate-mapping>

main

package test;

import org.hibernate.Session;
import org.hibernate.Transaction;

import util.HibernateUtil;
import dto.Address;
import dto.User;

public class UserTest {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		Address a = new Address();
		a.setAddressinfo("america");

		Address a2 = new Address();
		a2.setAddressinfo("china");

		User user = new User();
		user.setAccount("jack");
		user.setPassword("111111");

		user.getAddresses().add(a);
		user.getAddresses().add(a2);

		session.save(a);
		session.save(a2);
		session.save(user);

		tx.commit();
		HibernateUtil.closeSession();
	}

}


你可能感兴趣的:(java,Hibernate,ssh)