[英]org.hibernate.PropertyAccessException while persisting ManyToMany relationship
在像这样映射一个ManyToMany关系时存在问题
Document.java
public class Document {
.......
@ManyToMany(targetEntity = Category.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "fideuram_gup_documents_in_categories",
joinColumns = @JoinColumn(name="fk_document"),
inverseJoinColumns = @JoinColumn(name = "fk_category"))
private Set<Category> categories = new HashSet<Category>();
.......
}
其中,类别是我模型的另一个实体,由于它不带有此关系的反向映射,并且没有ID和名称,因此我不会在此处粘贴。
但是,当我尝试保留文档时,出现以下错误:
org.hibernate.PropertyAccessException: could not get a field value by reflection getter of it.ardesia.fideuram.gup.model.Category.id
我已经在网上冲浪了,但是没有页面与ManyToMany关系相关。 当然,我在实体Document上拥有的所有ManyToOne关系都可以正常工作。
我正在使用:
spring-data-jpa:1.2.0.RELEASE
hibernate-core:4.2.2.Final
hibernate-entitymanager:4.2.2.final
更新
所有实体都为每个字段公开默认的构造函数和getter / setter。 或者,更确切地说,我正在使用Spring Roo创建实体,并在编译时自动注入getter和setter。
您可以使用@javax.persistence.Access
注释来说明Hibernate如何必须访问您的属性。 将@Access.value
设置为
AccessType.FIELD
用于直接字段访问 AccessType.PROPERTY
用于使用访问器访问属性 也许可以帮到您,我已经做了同样的事情,我放了我的代码,它创建了一个联接表:
@Entity
@Table(name = "custom_pizza")
public class CustomPizza extends BaseEntity {
private static final long serialVersionUID = 1L;
// ManyToMany instead of oneToMany in order to don't have the unique
// constraint on each primary key of the join table
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "custom_pizza_topping", joinColumns = @JoinColumn(name = "custom_pizza_id"), inverseJoinColumns = @JoinColumn(name = "topping_id"))
private Set<Topping> toppings = new HashSet<Topping>();
public void addTopping(Topping topping) {
toppings.add(topping);
}
public void removeTopping(Topping topping) {
toppings.remove(topping);
}
...
而我的摘心:
@Entity
@Table(name = "topping")
public class Topping extends BaseEntity {
private static final long serialVersionUID = 1L;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "price", nullable = false)
private float price;
....
和BaseEntity
@MappedSuperclass
public abstract class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.