org.hibernate.PropertyAccessException同时持久化ManyToMany关系

时间:2014-02-26 15:15:29

标签: java hibernate jpa spring-data

持续存在像此一样映射的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>();
    .......
}

其中Category是我模型的另一个实体,我没有粘贴在这里,因为它没有这个关系的反向映射,只有一个ID和一个名字。

当我尝试保留Document时,我收到以下错误:

org.hibernate.PropertyAccessException: could not get a field value by reflection getter of it.ardesia.fideuram.gup.model.Category.id

我在网上浏览过但没有与ManyToMany关系相关的页面。当然,我在实体文档上的所有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。

2 个答案:

答案 0 :(得分:0)

您可以使用@javax.persistence.Access注释来检测Hibernate必须如何访问您的媒体资源;在@Access.value设置为

的情况下放置您的映射类
  1. AccessType.FIELD用于直接字段访问
  2. AccessType.PROPERTY用于使用访问者访问属性

答案 1 :(得分:0)

也许它可以帮助你,我已经做了同样的事情,我把我的代码,它创建了一个连接表:

@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;
  ...
相关问题