2014-02-26 49 views
0

有持續映射像org.hibernate.PropertyAccessException而持續多對多關係

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 

我衝浪關於它的網頁,但沒有頁面涉及到多對多的關係。當然,我對實體文檔的所有ManyToOne關係都可以正常工作。

我使用:

spring-data-jpa:1.2.0.RELEASE 
hibernate-core:4.2.2.Final 
hibernate-entitymanager:4.2.2.final 

UPDATE

所有實體暴露每個字段默認構造函數和getter/setter方法。或者,更精確的是,我使用Spring Roo來創建實體,並在編譯時自動注入getters和setter。

+0

是你能夠在根本上解決這個問題呢? – cooler

+1

@cooler我確實做到了,儘管我不能完全記得如何。如果我記得正確的話,這個問題與代碼(或元代碼)沒有關聯。這是關於創建衝突的傳遞依賴關係(導入了不同的JPA規範版本) –

回答

0

您可以使用@javax.persistence.Access註釋來測試Hibernate如何訪問您的財產;把你的映射類@Access.value設置爲

  1. AccessType.FIELD直接字段訪問
  2. AccessType.PROPERTY使用存取
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