hibernateexception:org.hibernate.AnnotationException:没有为实体指定标识符:com..domain.idea.MAE_MFEView

为什么我得到这个exception?

package com.domain.idea; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; import org.hibernate.annotations.AccessType; /** * object model for the view [InvestmentReturn].[vMAE_MFE] */ @Entity @Table(name="vMAE_MFE", schema="InvestmentReturn") @AccessType("field") public class MAE_MFEView { /** * trade property is a SuggestdTradeRecommendation object */ @OneToOne(fetch = FetchType.LAZY , cascade = { CascadeType.PERSIST }) @JoinColumn(name = "suggestedTradeRecommendationID") private SuggestedTradeRecommendation trade; /** * Most Adeverse Excursion value */ private int MAE; public int getMAE() { return MAE; } /** * Most Favorable Excursion value */ private int MFE; public int getMFE() { return MFE; } /** * @return trade property * see #trade */ public SuggestedTradeRecommendation getTrade() { return trade; } } 

更新:我已经改变了我的代码看起来像这样:

 package com.domain.idea; import javax.persistence.CascadeType; import javax.persistence.FetchType; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; import org.hibernate.annotations.AccessType; /** * object model for the view [InvestmentReturn].[vMAE_MFE] */ @Entity @Table(name="vMAE_MFE", schema="InvestmentReturn") @AccessType("field") public class MAE_MFEView { /** * trade property is a SuggestdTradeRecommendation object */ @Id @OneToOne(fetch = FetchType.LAZY , cascade = { CascadeType.PERSIST }) @JoinColumn(name = "suggestedTradeRecommendationID") private SuggestedTradeRecommendation trade; /** * Most Adeverse Excursion value */ private int MAE; public int getMAE() { return MAE; } /** * Most Favorable Excursion value */ private int MFE; public int getMFE() { return MFE; } /** * @return trade property * see #trade */ public SuggestedTradeRecommendation getTrade() { return trade; } } 

但现在我得到这个例外:

 Caused by: org.hibernate.MappingException: Could not determine type for: com.domain.idea.SuggestedTradeRecommendation, at table: vMAE_MFE, for columns: [org.hibernate.mapping.Column(trade)] at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:292) at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:276) at org.hibernate.mapping.RootClass.validate(RootClass.java:216) at org.hibernate.cfg.Configuration.validate(Configuration.java:1135) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1320) at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867) at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669) ... 145 more 

您缺less用@Id注释的字段。 每个@Entity需要@Id – 这是数据库中的主键。

如果你不希望你的实体被保存在一个单独的表中,而是作为其他实体的一部分,你可以使用@Embeddable而不是@Entity

如果你只需要一个数据传输对象来保存Hibernate实体中的一些数据,那么不要使用任何注释 – 只需要一个简单的pojo即可。

更新:关于SQL视图,Hibernate文档写入:

Hibernate映射的视图和基表之间没有区别。 这在数据库级别是透明的

下面的代码可以解决NullPointerException。

 @Id @GeneratedValue @Column(name = "STOCK_ID", unique = true, nullable = false) public Integer getStockId() { return this.stockId; } public void setStockId(Integer stockId) { this.stockId = stockId; } 

如果你添加@Id ,那么你可以声明一些更像上面声明的方法。

对我来说,应该使用javax.persistence.Id来代替org.springframework.data.annotation.Id 。 对于遇到此问题的任何人,您可以检查是否导入了正确的Id类。