2016-12-17 90 views
-1

我嘗試使用Hibernate 4.1.5。 當我嘗試添加在社區新行,我有一個錯誤:Java休眠PropertyAccessException

Exception in thread "main" Hibernate: insert into users_communities (community_id, user_id) values (?, ?) 
dbService.DBException: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of dbService.dataSets.UsersDataSet.id 
    at dbService.DBService.addUser(DBService.java:290) 
    at accounts.AccountService.addUser(AccountService.java:131) 
    at main.Main.main(Main.java:83) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 
Caused by: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of dbService.dataSets.UsersDataSet.id 
    at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:60) 
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:346) 
    at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:4746) 
    at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:4465) 
    at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:243) 
    at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:293) 
    at org.hibernate.type.EntityType.getIdentifier(EntityType.java:537) 
    at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:165) 
    at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:899) 
    at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1308) 
    at org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:67) 
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465) 
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351) 
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350) 
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56) 
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258) 
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425) 
    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) 
    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177) 
    at dbService.DBService.addUser(DBService.java:286) 
    ... 7 more 
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Long field dbService.dataSets.UsersDataSet.id to java.lang.Long 
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) 
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) 
    at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58) 
    at sun.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36) 
    at java.lang.reflect.Field.get(Field.java:393) 
    at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:57) 
    ... 26 more 

這是我的實體類:

Entity : "community"

@Entity 
@Table(name = "community") 
public class CommunityDataSet implements Serializable { // Serializable Important to Hibernate! 
    private static final Long serialVersionUID = -8706689714326132798L; 

    @Column(name = "user_id") 
    private Long id; 

    @Id 
    @Column(name = "community_id", unique = true, updatable = false) 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long community_id; 

    @Column(name = "community_name") 
    private String community_name; 

    @ManyToMany(targetEntity = UsersDataSet.class, fetch = FetchType.LAZY) 
    @JoinTable(name = "users_communities", joinColumns = @JoinColumn(name = "community_id"), 
     inverseJoinColumns = @JoinColumn(name = "user_id")) 
    private Set<Long> users = new HashSet<>(); 

    //Important to Hibernate! 
    @SuppressWarnings("UnusedDeclaration") 
    public CommunityDataSet() { 
    } 

    public CommunityDataSet(String community_name) { 
     //this.setId(id); 
     //this.setCommunityId(community_id); 
     this.setCommunityName(community_name); 
    } 

    public CommunityDataSet(Long user_id, String community_name) { 
     this.setUser(user_id); 
     //this.setCommunityId(community_id); 
     this.setCommunityName(community_name); 
    } 

    @SuppressWarnings("UnusedDeclaration") 


    public Long getId() { return id; } 

    private void setId(Long id) { this.id = id; } 

    public Long getCommunityId() { return community_id; } 

    private void setCommunityId(Long community_id) { 
     this.community_id = community_id; 
    } 

    public String getCommunityName() { return community_name;} 

    public void setCommunityName(String name) { this.community_name = name;} 

    public Set<Long> getUsers() { return users; } 

    //public void setUsers(Set<Long> users) { this.users = users; } 

    public void setUser(Long user) { 
     this.users.add(user); 
    } 

    public String toString() { 
     return "CommunityDataSet{" + 
      // "id=" + id + 
       ", community_id='" + community_id + '\'' + 
      '}'; 
    } 
} 

Entity = "users"

@Entity 
@Table(name = "users") 
public class UsersDataSet implements Serializable { // Serializable Important to Hibernate! 
    private static final Long serialVersionUID = -8706689714326132798L; 

    @Id 
    @Column(name = "user_id") 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id = 0L; 

    @Column(name = "login", unique = true, updatable = false) 
    private String login; 

    @Column(name = "password", updatable = false) 
    private String password; 

    @Column (name = "name") 
    private String name; 

    @Column (name = "surname") 
    private String surname; 

    @ManyToMany(targetEntity = CommunityDataSet.class, mappedBy="users", fetch = FetchType.LAZY) 
private Set<Long> communities = new HashSet<>(); 

    //Important to Hibernate! 
    @SuppressWarnings("UnusedDeclaration") 
    public UsersDataSet() { } 

    public UsersDataSet(Long id, String login, String password, String name, String surname) { 
     this.setId(id); 
     this.setLogin(login); 
     this.setPassword(password); 
     this.setName(name); 
     this.setSurname(surname); 
    } 

    @SuppressWarnings("UnusedDeclaration") 
    public UsersDataSet(Long id, String login, String password) { 
     this.setId(id); 
     this.setLogin(login); 
     this.setPassword(password); 
     this.setName(login); 
     this.setSurname(""); 
    } 

    public UsersDataSet(String login, String password) { 
     this.setId(++this.id); 
     this.setLogin(login); 
     this.setPassword(password); 
     this.setName(login); 
     this.setSurname(""); 
    } 

    @SuppressWarnings("UnusedDeclaration") 

    public Long getId() { return id; } 

    private void setId(Long id) { this.id = id; } 

    public String getLogin() { return login; } 

    public void setLogin(String login) { this.login = login; } 

    public String getPassword() { return password; } 

    public void setPassword(String password) { this.password = password; } 

    public String getName() { return name; } 

    public void setName(String name) { this.name = name; } 

    public String getSurname() { return surname; } 

    public void setSurname(String surname) { this.surname = surname; } 

    public Set<Long> getCommunities() { return communities; } 

    public void setCommunities(Set<Long> communities) { this.communities = communities; } 

    public void setCommunity(Long community_id) { this.communities.add(community_id); } 

    @Override 
    public String toString() { 
     return "UserDataSet{" + 
      "id=" + id + 
      ", login='" + login + '\'' + 
      '}'; 
    } 
} 

回答

0

給出答案之前有一些要點。

1-是它不是一個Ë ntity類重要一Ĵ AVA EAN即實現序列化(標記接口 - 一個空的接口)。

2-如果你希望它是一個合法的Ĵ AVA EAN然後確保你擁有所有領域私人(你這樣做的話),使所有存取(干將())和mutators(setter())方法public {Java Bean或簡單的POJO},並且還有一個默認的public構造函數。

現在對問題..

Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Long field dbService.dataSets.UsersDataSet.id to java.lang.Long

讓你的setter方法爲setId()公衆。

+1

Hibernate會很好地工作,而不getter和即使這些字段是私人的。 –

+0

感謝:)指出。 –

2

的問題是與此映射:

@ManyToMany(targetEntity = UsersDataSet.class, fetch = FetchType.LAZY) 
    @JoinTable(name = "users_communities", 
        joinColumns = @JoinColumn(name = "community_id"), 
     inverseJoinColumns = @JoinColumn(name = "user_id")) 
    private Set<Long> users = new HashSet<>(); 

這應該是如下,即要映射對象的關聯集合不是原始類型:

@ManyToMany(fetch = FetchType.LAZY) 
    @JoinTable(name = "users_communities", 
        joinColumns = @JoinColumn(name = "community_id"), 
     inverseJoinColumns = @JoinColumn(name = "user_id")) 
    private Set<UsersDataSet> users = new HashSet<>(); 
+0

非常感謝!有用。 –