今天在做项目的时候,一个中间表没有主键,所有在创建实体的时候也未加组件,结果报以下错误:
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类即可。
Hibernate注解映射联合主键的三种主要方式
原文地址:https://www.cnblogs.com/xdot/p/8686287.html
知识推荐
- HTML5数据储存
- salad~postcss外
- html5的web存储与cookie的区别
- net 总数据中取随机几条数据
- JS使用 popstate 事件监听物理返回键
- 【转载】Ubuntu16.04安装最新版nodejs
- 纯CSS+HTML制作网页加载动画
- php 监控文件变化 并上传到服务器
- HTML笔记
- Web网站服务
- zabbix日常监控项web(八)
- js短信验证倒计时
- 使用JS对form的内容验证失败后阻止提交
- nodejs http2https hbs
- phpstorm集成phpunit
- ExtJs的Reader
- php-fpm的pool php-fpm慢执行日志 open_basedir ?php-fpm进程管理
- jsf和facelets的生命周期