[英]org.hibernate.PropertyAccessException while persisting ManyToMany relationship
Have a problem persisting a ManyToMany relationship mapped like that 在像这样映射一个ManyToMany关系时存在问题
Document.java 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>();
.......
}
where Category is one more entity of my model which I don't paste here since it doesn't carry a reverse mapping of this relation, and has just an ID and a name. 其中,类别是我模型的另一个实体,由于它不带有此关系的反向映射,并且没有ID和名称,因此我不会在此处粘贴。
When I try to persist Document however I get the following error: 但是,当我尝试保留文档时,出现以下错误:
org.hibernate.PropertyAccessException: could not get a field value by reflection getter of it.ardesia.fideuram.gup.model.Category.id
I've surfed the web about it but no page relates to ManyToMany relations. 我已经在网上冲浪了,但是没有页面与ManyToMany关系相关。 Of course all the ManyToOne relations I have on the entity Document work fine.
当然,我在实体Document上拥有的所有ManyToOne关系都可以正常工作。
I'm using: 我正在使用:
spring-data-jpa:1.2.0.RELEASE
hibernate-core:4.2.2.Final
hibernate-entitymanager:4.2.2.final
UPDATE 更新
All entities expose a default constructor and getter/setter for every field. 所有实体都为每个字段公开默认的构造函数和getter / setter。 Or,more preciselt, I'm using Spring Roo for creating the entity and it injects getters and setters automatically upon compilation.
或者,更确切地说,我正在使用Spring Roo创建实体,并在编译时自动注入getter和setter。
You can instrument Hibernate how it must access your property using @javax.persistence.Access
annotation; 您可以使用
@javax.persistence.Access
注释来说明Hibernate如何必须访问您的属性。 put on your mapped class with @Access.value
set to 将
@Access.value
设置为
AccessType.FIELD
for direct field access AccessType.FIELD
用于直接字段访问 AccessType.PROPERTY
for accessing properties using accessors AccessType.PROPERTY
用于使用访问器访问属性 Maybe it can help you, I already did the same, I put my code, it creates a join table: 也许可以帮到您,我已经做了同样的事情,我放了我的代码,它创建了一个联接表:
@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);
}
...
And my 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;
....
and the BaseEntity 和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.