Java hibernate属性访问异常
我尝试使用Hibernate4.1.5。 当我尝试在社区中添加新行时,出现了一个错误:Java hibernate属性访问异常,hibernate,jpa,Hibernate,Jpa,我尝试使用Hibernate4.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 reflect
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
@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
@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 + '\'' +
'}';
}
}
在给出答案之前有几点
1-
实体类成为JavaBean并不重要,即实现可序列化的(标记接口-空接口)
2-
如果您希望它是合法的JavaBean,那么请确保您拥有所有字段私有的(您已经做到了),使所有访问器(getter())和变异器(setter())方法公共的{javabean或simple POJO},还有一个默认的公共构造函数
现在面临问题。
原因:java.lang.IllegalArgumentException:无法将java.lang.Long字段dbService.dataset.UsersDataSet.id设置为java.lang.Long
@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
@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 + '\'' +
'}';
}
}
将
setId()
的setter方法公开。此映射存在问题:
@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(targetEntity=UsersDataSet.class,fetch=FetchType.LAZY)
@JoinTable(name=“users\u communities”,
joinColumns=@JoinColumn(name=“community_id”),
inverseJoinColumns=@JoinColumn(name=“user\u id”))
private Set 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<>();
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name=“users\u communities”,
joinColumns=@JoinColumn(name=“community_id”),
inverseJoinColumns=@JoinColumn(name=“user\u id”))
private Set users=new HashSet();
Hibernate在没有getter和setter的情况下可以很好地工作,即使字段是私有的。