JPA enity有三次这种相同的关系

时间:2015-05-12 09:57:04

标签: java jpa jpa-2.0 jpql

我有两个简单的模型:

public class Wallet{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "WALLET_ID")
    private long walletId;

    @Column(name = "OWNER_LABEL")
    private String ownerLabel;

    @Column(name = "USER_ID")
    private long userId;
}

public class Transfer{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "TRANSFER_ID")
    private long transferId;

    @NotNull
    @Column(name = "AMOUNT")
    private BigDecimal amount = BigDecimal.ZERO;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "OWNER_WALLET_ID")
    private Wallet ownerWallet;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "FROM_WALLET_ID")
    private Wallet fromWallet;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "TO_WALLET_ID")
    private Wallet toWallet;
}

问题是当我想在一个查询中转移他所有的关系(钱包)时

SELECT t FROM Transfer t 
LEFT JOIN FETCH t.ownerWallet w 
LEFT JOIN FETCH t.toWallet tw 
LEFT JOIN FETCH t.fromWallet fw 
WHERE w.userId = :userId ORDER BY t.createdAt DESC

我得到异常 MySQLIntegrityConstraintViolationException :键'PRIMARY'的重复条目'2' 你有一些灵魂吗?谢谢你的建议

1 个答案:

答案 0 :(得分:0)

我非常确定您在尝试插入某些数据并使主键重复时遇到此错误。 MySQLIntegrityConstraintViolationException是关于DB模型中的约束违规,与选择数据无关。