Hibernate 实现复合主键的使用

转发自 https://blog.csdn.net/guominjin/article/details/78963578

  1>:首先将要作为主键的属性首先抽取成单独的实体  并提供get And set方法  并且还需要序列化

      比如现在有一个实体 People ,具有属性pName,pAddress,pJavaScore,pCscore

     复合主键为pName,pAddress

   **********将这两个属性抽离形成一个新的实体并提供setter,gettter方法实现序列化(坑区)

compositionkey.java


  
  
  1. import java.io.Serializable;
  2. /**
  3. *
  4. * @author Guomin JIn
  5. *
  6. */
  7. public class CompositionKey implements Serializable{
  8. private String pName;
  9. private String pAddress;
  10. public String getpName() {
  11. return pName;
  12. }
  13. public void setpName(String pName) {
  14. this.pName = pName;
  15. }
  16. public String getpAddress() {
  17. return pAddress;
  18. }
  19. public void setpAddress(String pAddress) {
  20. this.pAddress = pAddress;
  21. }
  22. }

  2>:在实际的实体中申明主键实体的变量

抽离之后的Peopel实体如下:

People.java


  
  
  1. package cn.pzhu.composition;
  2. /**
  3. * 实体类
  4. * @author Guomin JIn
  5. *
  6. */
  7. public class People {
  8. private CompositionKey key;
  9. private float pJavaScore;
  10. private float pCScore;
  11. public CompositionKey getKey() {
  12. return key;
  13. }
  14. public void setKey(CompositionKey key) {
  15. this.key = key;
  16. }
  17. public float getpJavaScore() {
  18. return pJavaScore;
  19. }
  20. public void setpJavaScore(float pJavaScore) {
  21. this.pJavaScore = pJavaScore;
  22. }
  23. public float getpCScore() {
  24. return pCScore;
  25. }
  26. public void setpCScore(float pCScore) {
  27. this.pCScore = pCScore;
  28. }
  29. @Override
  30. public String toString() {
  31. return "People [key=" + key + ", pJavaScore=" + pJavaScore + ", pCScore=" + pCScore + "]";
  32. }
  33. }

扫描二维码关注公众号,回复: 6178553 查看本文章

  3>:在映射文件中编写实体的配置文件

配置文件如下:

hibernate.cfg.xml省略()

people.hbm.xml


  
  
  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping package="cn.pzhu.composition">
  6. <class name="People" table="peopel" >
  7. <composite-id name="key">
  8. <key-property name="pName" length="10" type="java.lang.String"> </key-property>
  9. <key-property name="pAddress" length="10" type="java.lang.String"> </key-property>
  10. </composite-id>
  11. <property name="pJavaScore" length="5"/>
  12. <property name="pCScore"> </property>
  13. </class>
  14. </hibernate-mapping>
4>:编写测试类

测试类使用junit


  
  
  1. @Test
  2. public void test_0001() {
  3. Session session= new Configuration()
  4. .configure( "hibernate.cfg.xml")
  5. .addClass(People.class)
  6. .buildSessionFactory()
  7. .openSession();
  8. session.beginTransaction();
  9. CompositionKey key= new CompositionKey();
  10. key.setpName( "jgm");
  11. key.setpAddress( "chengdu");
  12. People people= new People();
  13. people.setKey(key);
  14. people.setpJavaScore( 12.34f);
  15. people.setpCScore( 36.2f);
  16. //HibernateUtil.save(people);
  17. session.save(people);
  18. }



猜你喜欢

转载自blog.csdn.net/qq_40183281/article/details/90032243