【问题标题】:org.hibernate.PropertyAccessException while persisting ManyToMany relationshiporg.hibernate.PropertyAccessException 同时保持多对多关系
【发布时间】:2014-02-26 15:15:28
【问题描述】:

在保持这样映射的多对多关系时遇到问题

文档.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

我在网上浏览过它,但没有任何页面涉及多对多关系。当然,我在实体 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。

【问题讨论】:

  • 你能解决这个问题吗?
  • @cooler 我确实做到了,虽然我不记得是怎么做到的。如果我没记错的话,问题与代码(或元代码)无关。这与传递依赖有关,它造成了冲突(导入了不同的 JPA 规范版本)

标签: java hibernate jpa spring-data


【解决方案1】:

您可以使用 @javax.persistence.Access 注释来检测 Hibernate 如何访问您的属性;将@Access.value 设置为

  1. AccessType.FIELD 用于直接字段访问
  2. AccessType.PROPERTY 使用访问器访问属性

【讨论】:

    【解决方案2】:

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

    @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;
      ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 2010-12-20
      • 2011-03-07
      相关资源
      最近更新 更多