org.hibernate.PropertyAccessException:具有多对多关系

时间:2013-02-15 18:37:02

标签: hibernate jpa many-to-many

我在为多对多关系(用户和消息表)建立新表(UserMessage)时遇到了问题。当我尝试从映射表中访问一行时,我得到一个例外: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();

我的课程:

User.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);
    }
}

Message.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;
    }
}

UserMessage.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);
    }
}

UserMessagePK.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(){
    }
}

有什么问题?

2 个答案:

答案 0 :(得分:1)

问题出在查询中。

select distinct M from Message M join M.toUser t where t.pk.user = :user效果很好。

答案 1 :(得分:0)

我建议采用不同的方法。 在User类中,不要持有一组UserMessage,而是写下:

@Entity
@Table(name = "Users")
public class User implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "User_Id")
private Long userid;

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinTable(name = "USER_MESSAGE", joinColumns = @JoinColumn(name = "User_Id"), inverseJoinColumns = @JoinColumn(name = "Message_Id"))
protected Set<Message> userMessages = new HashSet<>(0);

public void addUserMessage(Message toAdd) {
if (null != toAdd) {
    this.userProviders.add(toAdd);
}
}

public void getUserMessages() {
return this.userMessages;
}

public void setUserMessages(Set<Message> userMessages) {
this.userMessages.clear();
this.userMessages.addAll(userMessages);
}

// add getter and setter to id

...

在Message类中,您应该:

@Entity
@Table(name = "User_Messages")
public class Message implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Message_Id")
protected long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinTable(name = "Users_Messages", joinColumns = @JoinColumn(name = "Message_Id", insertable = false, updatable = false), inverseJoinColumns = @JoinColumn(name = "User_Id"))
protected User user;

// add getters and setters
...

您也可以摆脱UserMessagePK类和UserMessage(为什么需要这些?)。 现在,当您生成表格时(可以使用hbm2ddl tool),您应该有一个用户&lt; - &gt;消息关系的连接表。

现在您可以运行查询。