Hibernate使用注解时的问题

在使用注解进行Hibernate开发时、报了一个如下的错误

Exception in thread "main" org.hibernate.AnnotationException: No identifier specified for entity: com.eyang.hibernate.model.Qygd
	at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:277)
	at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:224)
	at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:775)
	at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788)
	at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742)
	at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
	at QygdTest.main(QygdTest.java:23)

经查证是在Model类中没有给主键添加注解、也就是说在使用Hibernate进行开发时必须指定表的主键才能正常运行、

在对应属性的get方法上添加@Id注解即可

 

另外还发现了一个现象、即使用xml配置文件和注解进行数据的添加时

若表中有10个字段、Model中也有十个属性与之对应、

在xml文件中、我们可以只写其中的3个或者几个字段则生成的sql中只有对应的这几个字段的值、而没有配置的则不会在sql中生成、

在使用注解时、Model中的十个属性也会全部在sql中生成、

转载于:https://www.cnblogs.com/keryang/p/4262696.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值