• Hibernate注解映射联合主键的三种主要方式


      今天在做项目的时候,一个中间表没有主键,所有在创建实体的时候也未加组件,结果报以下错误:

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateSessionFactory' defined in class path resource [spring-config/ac-cif-srv-config.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: XXX

      可以看出,其指出某一类是未指定标识符的实体,其主要原因是hibernate在进行扫描实体的时候,为发现其主键标识。所以就在其类上添加主键标识。因为我的这个类比较特殊,需要添加联合主键。

      联合主键用Hibernate注解映射方式主要有三种:

      一、将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode,再将该类注解为@Embeddable,最后在主类中(该类不包含联合主键类中的字段)保存该联合主键类的一个引用,并生成set和get方法,并将该引用注解为@Id

    @Entity  
    @Table(name="Test01")  
    public class Test01 implements Serializable{  
      
        private static final long serialVersionUID = 3524215936351012384L;  
        private String address ;  
        private int age ;  
        private String email ;  
        private String phone ;  
        @Id
        private TestKey01 testKey ;
    }

      主键类:

    @Embeddable  
    public class Testkey01  implements Serializable{  
      
        private static final long serialVersionUID = -3304319243957837925L;  
        private long id ;  
        private String name ;  
        /** 
         * @return the id 
         */  
        public long getId() {  
            return id;  
        }  
        /** 
         * @param id the id to set 
         */  
        public void setId(long id) {  
            this.id = id;  
        }  
        /** 
         * @return the name 
         */  
        public String getName() {  
            return name;  
        }  
        /** 
         * @param name the name to set 
         */  
        public void setName(String name) {  
            this.name = name;  
        }  
      
        @Override  
        public boolean equals(Object o) {  
            if(o instanceof Testkey0101){  
                Testkey01 key = (TestKey01)o ;  
                if(this.id == key.getId() && this.name.equals(key.getName())){  
                    return true ;  
                }  
            }  
            return false ;  
        }  
          
        @Override  
        public int hashCode() {  
            return this.name.hashCode();  
        }  
          
    }  

      二、将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode,最后在主类中(该类不包含联合主键类中的字段)保存该联合主键类的一个引用,并生成set和get方法,并将该引用注解为@EmbeddedId

    @Entity  
    @Table(name="Test02")  
    public class Test02 {  
      
        private String address ;  
        private int age ;  
        private String email ;  
        private String phone ;  
        @EmbeddedId
        private TestKey02 testKey ;
    }

      Testkey02为普通Java类即可。

      三、将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并要重写equals和hashcode.最后在主类中(该类包含联合主键类中的字段)将联合主键字段都注解为@Id,并在该类上方将上这样的注解:@IdClass(联合主键类.class)

    @Entity  
    @Table(name="Test03")  
    @IdClass(TestKey03.class)  
    public class Test03 {  
        @Id
        private long id ;
        @Id  
        private String name ; 
    }

      Testkey03为普通Java类即可。

  • 相关阅读:
    乐视电视修改五大图标
    Google Home Mini配置失败解决办法
    使用FreeHttp任意篡改http报文 (FreeHttp使用及实现说明)
    IDEA和VS快捷键对比
    C# 中的字符串内插
    在线运行.NET代码
    在Windows服务器安装ss服务端用于逃脱公司行为管理
    .NET Core的代码安全分析工具
    用DotNetDetour HOOK .net类库
    C#代码获取另一程序的错误提示,并关闭窗口。
  • 原文地址:https://www.cnblogs.com/lcngu/p/5854864.html
Copyright © 2020-2023  润新知