hibernate复合主键

复习了下hibernate复合主键,贴上来已备复习。

hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory >
		<property name="show_sql">true</property>
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
		<property name="connection.driver_class">
		org.gjt.mm.mysql.Driver
		</property>
		<property name="connection.url">
			jdbc:mysql://localhost:3306/Sample
		</property>
		<property name="connection.username">
			root
		</property>
		<property name="connection.password">
			root
		</property>
		
		<mapping resource="com/user/compositeid/User.hbm.xml" />
		
	
		
	</session-factory>
</hibernate-configuration>



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">
<hibernate-mapping>
	<class name="com.user.compositeid.User" table="user">
		<composite-id name="userPk" class="com.user.compositeid.UserPK">
			<key-property name="firstname" column="firstname" type="string"/>
			<key-property name="lastname" column="lastname" type="string"/>
		</composite-id>
		<property name="age" column="age" type="int"/>
	
	</class>


</hibernate-mapping>
                           


User.java
package com.user.compositeid;

import java.io.Serializable;

public class User implements Serializable{
	private UserPK userPk;
	private int age;
	public UserPK getUserPk() {
		return userPk;
	}
	public void setUserPk(UserPK userPk) {
		this.userPk = userPk;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}

}



UserPK.java
package com.user.compositeid;

import java.io.Serializable;

public class UserPK implements Serializable{

	private String firstname;
	private String lastname;
	
	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((firstname == null) ? 0 : firstname.hashCode());
		result = prime * result
				+ ((lastname == null) ? 0 : lastname.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final UserPK other = (UserPK) obj;
		if (firstname == null) {
			if (other.firstname != null)
				return false;
		} else if (!firstname.equals(other.firstname))
			return false;
		if (lastname == null) {
			if (other.lastname != null)
				return false;
		} else if (!lastname.equals(other.lastname))
			return false;
		return true;
	}

	
	
	
	
}



UserTest.java
package com.user.compositeid;

import java.util.Iterator;
import java.util.List;

import junit.framework.TestCase;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class UserTest extends TestCase {

	Configuration conf;
	SessionFactory factory;
	Session session;
	Transaction tx;

	// 自动创建数据库

	public void testCreate() {
		SchemaExport export = new SchemaExport(conf);
		export.create(true, true);

	}

	public void testAddUser() {
		factory = conf.buildSessionFactory();
		session = factory.openSession();
		tx = session.beginTransaction();
		User user = new User();
		UserPK userPK = new UserPK();
		userPK.setFirstname("zhang");
		userPK.setLastname("三");
		user.setUserPk(userPK);
		user.setAge(12);

		session.save(user);
		tx.commit();

		session.close();
		factory.close();

	}

	public void testLoadUser() {
		factory = conf.buildSessionFactory();
		session = factory.openSession();
		User user=new User();
		UserPK userPK = new UserPK();


		userPK.setFirstname("zhang");
		//这里是User.class,不是UserPK.class
		user=(User)session.load(User.class , userPK);
		System.out.println("firstname:"+user.getUserPk().getFirstname());
		
	    session.close();
		factory.close();
	}

	@Override
	protected void setUp() throws Exception {
		conf = new Configuration()
				.configure("com/user/compositeid/hibernate.cfg.xml");

	}
}

你可能感兴趣的:(java,Hibernate,mysql,xml,JUnit)