Java hibernate PropertyAccessException

时间:2016-12-17 05:08:45

标签: hibernate jpa

我尝试使用Hibernate 4.1.5。 当我尝试在社区中添加新行时,我遇到了错误:

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 + '\'' +
            '}';
    }
}

2 个答案:

答案 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<>();

答案 1 :(得分:0)

在回答之前有一些要点。

1- E ntity 类对于并不重要> J ava B ean 即实施 Serializable (Marker接口 - 一个空接口。)

2- 如果您希望它是合法的 J ava B ean 然后确保你拥有所有字段私有(你已经完成),制作所有访问者( getters())和mutators( setters( ))方法公共 {Java Bean或简单POJO},并且还有一个默认的公共构造函数。

现在面临问题..

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

setId() 的setter方法设为公开。