Hibernate双向多对一

时间:2013-01-09 10:55:09

标签: hibernate hibernate-mapping

我遇到以下错误的问题:

Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/library.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: A Foreign key refering tv.mirada.connect.cashless.parking.model.PaymentInterface from tv.mirada.connect.cashless.parking.model.Merchant has the wrong number of column. should be 0

我花了大约一天的时间寻找答案和尝试,没有运气。我实际上并不需要双向访问,我只需要能够从payment_interface获取商家表行,但是包含双向而不是尝试从单向访问到单向访问似乎更简单。

我使用的表是商家表和付款界面表。我意识到我可以让商家表直接引用节点表,但是商家表在支付界面中有一个信息的扩展,所以以这种方式映射它更有意义。

@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@Table(name = "park_merchant")
public class Merchant implements java.io.Serializable {

    @Id
    @GeneratedValue
    @Column(name="id", unique=true, nullable=false)
    private Integer id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="payment_interface_node_id", nullable = false)
    private PaymentInterface paymentInterface;


@Entity
@Table(name = "park_payment_interface", uniqueConstraints = @UniqueConstraint(columnNames = "name"))
public class PaymentInterface implements java.io.Serializable {

    @Id
    @OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    @JoinColumn(name = "node_id", unique = true, nullable = false)
    private Node node;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "paymentInterface")
    private Set<Merchant> merchants = new HashSet<Merchant>(0);

希望我只是遗漏了一些简单的东西。

1 个答案:

答案 0 :(得分:2)

啊,找到了解决方案。我需要将@ManyToOne和@JoinColumn放在getter上的Merchant表中,而不是放在变量声明上。我仍然不确定为什么,但至少我现在知道了。