OneToOne映射不适用于抽象子类

时间:2011-12-29 23:44:53

标签: hibernate mapping one-to-one

我遇到了令人沮丧的地图问题,肯定需要一些眼球。 我有一个抽象类,其子类具有单表的继承策略。 它拥有一个联系人,并且还需要一个具有相同继承策略的抽象类。 这是我的代码:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
@Table(name = "leads")
@Cache(region = "com.qe.model.Lead", usage = CacheConcurrencyStrategy.READ_WRITE)
public abstract class Lead {
    private Long id;
    protected Contact contact;


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    public Long getId() {
        return id;
    }

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

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "lead")
    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

}


@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
@Table(name = "contacts")
@Cache(region = "com.qe.model.Contact", usage = CacheConcurrencyStrategy.READ_WRITE)
public abstract class Contact {

    private Long id;
    private Lead lead;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    public Long getId() {
        return id;
    }

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

    @OneToOne
    @JoinColumn(name = "lead_id")
    public Lead getLead() {
        return lead;
    }

    public void setLead(Lead lead) {
        this.lead = lead;
    }
}
@Entity
@DiscriminatorValue("AUTO")
public class AutoContact extends Contact {
}

我收到错误:

  

无法确定:com.qe.model.AutoContact的类型,在表格中:   对于列:[org.hibernate.mapping.Column(contact)]

任何想法都会受到赞赏......

1 个答案:

答案 0 :(得分:0)

对不起,实际上你的映射对我来说非常好。错误消息令我感到困惑,因为contact表中显然不应该有列leads。您的映射定义了contacts表中的外键列。 Hibernate也应该支持多态一对一关联。

您可以尝试将targetEntity参数添加到类ContactLead的映射中(尽管我怀疑这会有所帮助)。

@OneToOne(cascade = CascadeType.ALL, mappedBy = "lead", targetEntity = Contact.class)
public Contact getContact() {
  return contact;
}

您还可以尝试从类abstract中删除Contact修饰符(尽管这可能不是您想要做的)。

希望有所帮助。