方法对象Hibernate联合主键

文章结束给大家来个程序员笑话:[M]

         有时候我们在项目中可能要需结合主键来现实我们的业务,比如说 对于User类说来,我们可以把cardId和name它们两个结合起来作为User类的主键。

        官方API给我们解释的如下:

         You can use a component as an identifier of an entity class. Your component class must satisfy certain requirements:

          1.它必须现实java.io.Serializable接口

          2.It must re-implement equals() and hashCode() consistently with the database's notion of composite key equality.

          通过面上的我们可以晓得,结合主键的实体必须现实Serializable接口以及写重equals()和hashCode()方法。

       为什么要这样做呢?

       写重这两个方法是因为hibernate要根据数据库的结合主键来断判某两行记载是不是是一样的,如果一样那么就认为是同一个对象,如果不一样,那么认为是不同的对象,这反应到程序域领中就是根据hashCode与equals方法来断判某两个对象是不是可以放到诸如Set这样的集合当中。
   结合主键的实体类现实Serializable接口的原因在于用使get或load方法的时候要需先构建出来该实体的对象,并且将查询据依(结合主键)置设进去,然后作为get或load方法第二个数参传进去便可。

       通过例子应当更好的助帮我们解理:

        User.java 用户实体类

     

package com.bawei.demo.vo;

public class User {

	private UserPK id;
	public UserPK getId() {
		return id;
	}
	public void setId(UserPK id) {
		this.id = id;
	}
	private String sex;
	private String address;
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", sex=" + sex + ", address=" + address + "]";
	}
	
}

    UserPK.java

    

package com.bawei.demo.vo;

import java.io.Serializable;

/**
 * @author shenyiya
 * 实体主键 
 *
 */
public class UserPK implements Serializable {

	private String cardId;
	private String name;

	public String getCardId() {
		return cardId;
	}

	public void setCardId(String cardId) {
		this.cardId = cardId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((cardId == null) ? 0 : cardId.hashCode());
		result = prime * result + ((name == null) ? 0 : name.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;
		}
		UserPK other = (UserPK) obj;
		if (cardId == null) {
			if (other.cardId != null) {
				return false;
			}
		} else if (!cardId.equals(other.cardId)) {
			return false;
		}
		if (name == null) {
			if (other.name != null) {
				return false;
			} else if (!name.equals(other.name)) {
				return false;
			}
		}
		return true;
	}

}

    User.hbm.xml  实体映射文件

 

    每日一道理
微笑,是春天里的一丝新绿,是秋日里的一缕阳光,是骄阳下的一片浓荫,是冬雪中的一株梅红……微笑着去面对吧,你会感到人生是那样的温馨与甜蜜!
<?xml version="1.0" encoding="gb2312"?> 
<!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.bawei.demo.vo.User" table="user">
		<composite-id name="id" class="com.bawei.demo.vo.UserPK">
			<key-property name="cardId" type="string"></key-property>
			<key-property name="name" type="string"></key-property>
		</composite-id>
		<property name="sex"></property>
		<property name="address"></property>

	</class>
</hibernate-mapping>

    Hibernate.cfg.xml hibernate配置文件

<?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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
	<property name="dialect">
		org.hibernate.dialect.MySQLDialect
	</property>
	<property name="connection.url">
		jdbc:mysql://localhost:3306/demo
	</property>
	<property name="connection.username">root</property>
	<property name="connection.password">123</property>
	<property name="connection.driver_class">
		com.mysql.jdbc.Driver
	</property>
	<property name="myeclipse.connection.profile">conn</property>
	<property name="show_sql">false</property>
	<property name="hbm2ddl.auto">update</property>
	<mapping resource="com/bawei/demo/dao/Author.hbm.xml" />
	<mapping resource="com/bawei/demo/dao/Post.hbm.xml" />
	<mapping resource="com/bawei/demo/dao/User.hbm.xml" />

</session-factory>

</hibernate-configuration>

 

    Main 函数 :

public static void main(String[] args) {
		// 1.初始化配置文件
		Configuration configuration = new Configuration().configure();
		// 2.立建session工厂
		SessionFactory sessionFactory = configuration.buildSessionFactory();
		// 3.从工厂中得取一个连接
		Session session = sessionFactory.openSession();
		// 4.开启务事
		Transaction tx = session.beginTransaction();
//      保存
//		UserPK usersPK = new UserPK();
//		usersPK.setName("zhangsan");
//		usersPK.setCardId("110");
//		User user = new User();
//		user.setId(usersPK);
//		user.setAddress("beijing");
//		user.setSex("M");
//		session.save(user);
		//查询 
		
		UserPK usersPK = new UserPK();
		usersPK.setName("zhangsan");
		usersPK.setCardId("110");
		User user=(User) session.load(User.class, usersPK);
		System.out.println(user.toString());
		
		

		// 5.提交务事
		tx.commit();
		// 6.闭关连接
		session.close();
	}

    效果如下图:

 

    
方法对象Hibernate联合主键方法对象Hibernate联合主键

 

 

 

    

 

 

 

 

     

     

文章结束给大家分享下程序员的一些笑话语录: 姿势要丰富,经常上百度!

你可能感兴趣的:(Hibernate)