使用EntityManager指定两次列

时间:2014-09-12 12:17:07

标签: java hibernate jpa orm

我试图坚持拥有复合主键的实体,但我收到错误:

12:59:48,221 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-56) SQL Error: 1110, SQLState: 42000

12:59:48,221 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-56) Column 'SENDERID' specified twice

我正在使用EntityManager,所以我不确定'SENDERID'在哪里被证实两次?

这是所有相关课程:

web服务:

@Path("friendservice")
public class FriendWebService {

    @EJB
    private FriendrequestFacade friendRequestFacade;

    @GET
    @Path("friendrequest")
    @Produces(MediaType.TEXT_PLAIN)
    public String insertFriendRequest(
                    @Context HttpServletRequest request){
        String result = "false";
        User user = (User) request.getSession().getAttribute("user");
        User otherUser = (User) request.getSession().getAttribute("profileuser");
        if((user != null) && (otherUser != null)){
            if(user.getId() != otherUser.getId()){
                System.out.println("Both users are alive.");
                if(friendRequestFacade.insertFriendRequest(user, otherUser))
                    result = "true";
            }
        }
        return result;
    }           
}

门面:

@Stateless
public class FriendrequestFacade extends AbstractFacade<Friendrequest> {
    @PersistenceContext(unitName = "FakebookPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public FriendrequestFacade() {
        super(Friendrequest.class);
    }

    public boolean insertFriendRequest(User user, User otherUser){
        Friendrequest fr = new Friendrequest();
        FriendrequestPK frPK = new FriendrequestPK();
        frPK.setSenderid(user.getId());
        frPK.setReceiverid(otherUser.getId());
        fr.setId(frPK);
        em.clear();
        em.persist(fr);
        return true;                
    }
}

实体:

@Entity
@XmlRootElement
@Table(name="FRIENDREQUEST")
@NamedQuery(name="Friendrequest.findAll", query="SELECT f FROM Friendrequest f")
public class Friendrequest implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private FriendrequestPK id;

    @Temporal(TemporalType.TIMESTAMP)
    private Date senddate;

    //bi-directional many-to-one association to User
    @ManyToOne
    @JoinColumn(name="SENDERID")
    private User user1;

    //bi-directional many-to-one association to User
    @ManyToOne
    @JoinColumn(name="RECEIVERID")
    private User user2;

    public Friendrequest() {}

    public FriendrequestPK getId() {
        return this.id;
    }

    public void setId(FriendrequestPK id) {
        this.id = id;
    }

    public Date getSenddate() {
        return this.senddate;
    }

    public void setSenddate(Date senddate) {
        this.senddate = senddate;
    }

    public User getUser1() {
        return this.user1;
    }

    public void setUser1(User user1) {
        this.user1 = user1;
    }

    public User getUser2() {
        return this.user2;
    }

    public void setUser2(User user2) {
        this.user2 = user2;
    }

}

复合键:

@Embeddable
public class FriendrequestPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(insertable=false, updatable=false)
    private int senderid;

    @Column(insertable=false, updatable=false)
    private int receiverid;

    public FriendrequestPK() {}

    public FriendrequestPK(int senderid, int receiverid){
        this.senderid = senderid;
        this.receiverid = receiverid;
    }

    public int getSenderid() {
        return this.senderid;
    }

    public void setSenderid(int senderid) {
        this.senderid = senderid;
    }

    public int getReceiverid() {
        return this.receiverid;
    }

    public void setReceiverid(int receiverid) {
        this.receiverid = receiverid;
    }
}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

首先请让我澄清一下,我很少使用@EmbeddedId所以我可能会遗漏一些东西。这被告知,错误告诉您SENDERID列被指定两次:第一次在您的实体中,然后在复合键中。 RECEIVERID也可能发生同样的情况。

实体

public class Friendrequest implements Serializable {
    ...
    @EmbeddedId
    private FriendrequestPK id;

    @ManyToOne
    @JoinColumn(name="SENDERID") // Column = SENDERID
    private User user1;

    @ManyToOne
    @JoinColumn(name="RECEIVERID") // Column = RECEIVERID
    private User user2;
    ...
}

复合键

public class FriendrequestPK implements Serializable {
    ...
    @Column(insertable=false, updatable=false)
    private int senderid; // Column = SENDERID

    @Column(insertable=false, updatable=false)
    private int receiverid; // Column = RECEIVERID
    ...
}

根据Mapping identifier properties参考指南中的Hibernate Annotations部分,实体映射应使用@MapsId注释完成:

public class Friendrequest implements Serializable {
    ...
    @EmbeddedId
    private FriendrequestPK id;

    @MapsId("senderid") // senderid = Field in FriendrequestPK class
    @ManyToOne
    private User user1;

    @MapsId("receiverid") // receiverid = Field in FriendrequestPK class
    @ManyToOne
    private User user2;
    ...
}