Hibernate多对多单向关联

模型user,address,user_address

user_address的userid和addressid为user,address的外键

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 ,
PRIMARY KEY (`addressid`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=38
ROW_FORMAT=COMPACT
;

user_address

CREATE TABLE `NewTable` (
`addressid`  int(11) NULL DEFAULT NULL ,
`userid`  int(11) NULL DEFAULT NULL ,
FOREIGN KEY (`addressid`) REFERENCES `address` (`addressid`) ON DELETE RESTRICT ON UPDATE RESTRICT,
FOREIGN KEY (`userid`) REFERENCES `user` (`userid`) ON DELETE RESTRICT ON UPDATE RESTRICT,
INDEX `userid` (`userid`) USING BTREE ,
INDEX `addressid` (`addressid`) USING BTREE 
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
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 0:13:09 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="user_address" inverse="false" lazy="true">
        	<key>
        		<column name="userid"></column>
        	</key>
        	<many-to-many class="dto.Address" column="addressid"  ></many-to-many>
        </set>
    </class>
</hibernate-mapping>

address

package dto;

public class Address {
	private String addressinfo;
	private int addressid;

	public String getAddressinfo() {
		return addressinfo;
	}

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

	public int getAddressid() {
		return addressid;
	}

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

}

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 0:13:09 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>
        <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 user2 = new User();
		user2.setAccount("peter");
		user2.setPassword("111111");
		
		user.getAddresses().add(a);
		user.getAddresses().add(a2);
		
		user2.getAddresses().add(a2);
		
		session.save(a);
		session.save(a2);
		session.save(user);
		session.save(user2);

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

}


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