我在为多对多关系(用户和消息表)建立新表(用户消息)时遇到了问题。当我尝试从映射表中访问一行时,出现异常:org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.package.model.User.userid
UserMessage 实体对于进一步向关系表添加属性是必需的。
数据通过以下方式访问:
Session session = (Session) entityManager.getDelegate();
String hql = "FROM Message M, UserMessage UM WHERE M.messageid = UM.pk.message and UM.pk.user = :id";
Query query = session.createQuery(hql);
query.setParameter("id", id);
query.list();
我的课程:
用户.java
@Entity
@Table(name = "USER")
public class User {
@Id @GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
@Column(nullable = false)
private Long userid;
@Column(unique = true, nullable = false)
private String username;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.user", cascade = {CascadeType.PERSIST})
private Set<UserMessage> inbox = new HashSet<UserMessage>();
public User() {
}
public Long getUserid() {
return userid;
}
public void setUserid(Long id) {
this.userid = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Set<UserMessage> getInbox() {
return inbox;
}
public void setInbox(Set<UserMessage> inbox) {
this.inbox = inbox;
}
public void addMessageToUser(Message message){
UserMessage userMessage = new UserMessage();
userMessage.setUser(this);
userMessage.setMessage(message);
inbox.add(userMessage);
}
}
消息.java
@Entity
@Table(name = "MESSAGE")
public class Message {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long messageid;
private String subject;
private String body;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "pk.message",cascade = {CascadeType.PERSIST})
private Set<UserMessage> toUser = new HashSet<UserMessage>();
public Set<UserMessage> getToUser() {
return toUser;
}
public void setToUser(Set<UserMessage> toUser) {
this.toUser = toUser;
}
public void addUserToMessage(User user){
UserMessage userMessage = new UserMessage();
userMessage.setUser(user);
userMessage.setMessage(this);
toUser.add(userMessage);
}
public Message() {
}
public Long getMessageid() {
return messageid;
}
public void setMessageid(Long id) {
this.messageid = id;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
用户消息.java
@Entity
@Table(name = "USER_MESSAGE")
@AssociationOverrides({
@AssociationOverride(name = "pk.user", joinColumns = @JoinColumn(name = "userid")),
@AssociationOverride(name = "pk.message", joinColumns = @JoinColumn(name = "messageid"))
})
public class UserMessage{
@EmbeddedId
private UserMessagePK pk = new UserMessagePK();
public UserMessagePK getPk(){
return pk;
}
public void setPk(UserMessagePK pk){
this.pk = pk;
}
public UserMessage(){
}
@Transient
public User getUser(){
return this.pk.getUser();
}
@Transient
public Message getMessage(){
return this.pk.getMessage();
}
public void setUser(User user){
this.pk.setUser(user);
}
public void setMessage(Message message){
this.pk.setMessage(message);
}
}
用户消息PK.java
@Embeddable
public class UserMessagePK implements Serializable {
private static final long serialVersionUID = 1L;
@ManyToOne
private User user;
@ManyToOne(cascade = CascadeType.REFRESH)
private Message message;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
public UserMessagePK(User user, Message message){
this.user = user;
this.message = message;
}
public UserMessagePK(){
}
}
怎么了?