今天,工作中遇到将mappedBy 注解加到 Getter公共属性上编译不通过,但是加到私有的属性字段上就可以编译通过的问题。
最后在Stack Overflow上找到答案。
原因是私有的private域 和公有的getter属性上注解混用造成的。(oderId域上有@Id注解,其他的注解全在公有的getter属性上)
I got the same problem with @ManyToOne
column. It was solved... in stupid way. I had all other annotations for public getter methods, because they were overridden from parent class. But last field was annotated for private variable like in all other classes in my project. So I got the same MappingException without the reason.
Solution: I placed all annotations at public getter methods. I suppose, Hibernate can't handle cases, when annotations for private fields and public getters are mixed in one class.
出错代码
package com.zx.stlife.entity.order;
import com.zx.stlife.constant.Const;
import com.zx.stlife.entity.SuperIntVersion;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "ngy_order")
public class NgyOrder{
public NgyOrder() {
}
private Set<NgyOrderItem> orderItemsSet;
private NgyOrderShipping ngyOrderShipping;
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "uuid")
private String orderId;
private String title;
private Double totalPayFee;
@OneToMany(mappedBy="ngyOrder",fetch=FetchType.LAZY)
public Set<NgyOrderItem> getOrderItemsSet() {
return orderItemsSet;
}
public void setOrderItemsSet(Set<NgyOrderItem> orderItemsSet) {
this.orderItemsSet = orderItemsSet;
}
@OneToOne(mappedBy="ngyOrder",fetch=FetchType.LAZY)
public NgyOrderShipping getNgyOrderShipping() {
return ngyOrderShipping;
}
public void setNgyOrderShipping(NgyOrderShipping ngyOrderShipping) {
this.ngyOrderShipping = ngyOrderShipping;
}
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Double getTotalPayFee() {
return totalPayFee;
}
public void setTotalPayFee(Double totalPayFee) {
this.totalPayFee = totalPayFee;
}
正确代码
package com.zx.stlife.entity.order;
import com.zx.stlife.constant.Const;
import com.zx.stlife.entity.SuperIntVersion;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "ngy_order")
public class NgyOrder{
public NgyOrder() {
}
private Set<NgyOrderItem> orderItemsSet;
private NgyOrderShipping ngyOrderShipping;
private String orderId;
private String title;
private Double totalPayFee;
@OneToMany(mappedBy="ngyOrder",fetch=FetchType.LAZY)
public Set<NgyOrderItem> getOrderItemsSet() {
return orderItemsSet;
}
public void setOrderItemsSet(Set<NgyOrderItem> orderItemsSet) {
this.orderItemsSet = orderItemsSet;
}
@OneToOne(mappedBy="ngyOrder",fetch=FetchType.LAZY)
public NgyOrderShipping getNgyOrderShipping() {
return ngyOrderShipping;
}
public void setNgyOrderShipping(NgyOrderShipping ngyOrderShipping) {
this.ngyOrderShipping = ngyOrderShipping;
}
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "uuid")
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Double getTotalPayFee() {
return totalPayFee;
}
public void setTotalPayFee(Double totalPayFee) {
this.totalPayFee = totalPayFee;
}