简体   繁体   English

org.hibernate.PropertyAccessException,同时保持ManyToMany关系

[英]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设置为

  1. AccessType.FIELD for direct field access AccessType.FIELD用于直接字段访问
  2. 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.

相关问题 org.hibernate.PropertyAccessException持续存在 - org.hibernate.PropertyAccessException on persist Java:调用setter和getter时org.hibernate.PropertyAccessException - Java : org.hibernate.PropertyAccessException while calling setter and getter 休眠:org.hibernate.PropertyAccessException:IllegalArgumentException - Hibernate : org.hibernate.PropertyAccessException: IllegalArgumentException Hibernate Criteria - org.hibernate.PropertyAccessException:IllegalArgumentException - Hibernate Criteria - org.hibernate.PropertyAccessException: IllegalArgumentException jComboBox给出org.hibernate.PropertyAccessException错误 - jComboBox giving org.hibernate.PropertyAccessException error org.hibernate.PropertyAccessException:空值已分配给布尔类型的属性 - org.hibernate.PropertyAccessException: Null value was assigned to a property of boolean type org.hibernate.PropertyAccessException-如何从数据库中获取空值? - org.hibernate.PropertyAccessException - How to get a null value out of database? 为什么会出现org.hibernate.PropertyAccessException? - Why am I getting org.hibernate.PropertyAccessException? org.hibernate.PropertyAccessException:无法通过反射getter获取字段值 - org.hibernate.PropertyAccessException: could not get a field value by reflection getter of org.hibernate.PropertyAccessException:使用CGLIB设置异常的属性值 - org.hibernate.PropertyAccessException: exception setting property value with CGLIB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM