如果列名不同,则一对多hibernate连接

时间:2012-06-22 12:30:37

标签: java hibernate annotations hibernate-mapping

我有三个表格具有以下结构 -

合同 - > contract_id(主要),customer_company_id,Vendor_company_id

公司 - > Company_id(主要),creation_date,created_by

Company_Timeline - > Timeline_id(主要),Company_id,Company_name。

并为这些表提供以下类 -

Contract.java

@Table(name = "CONTRACT")
@Entity
@SequenceGenerator(name = "ContractSeq", sequenceName = "SEQCONTRACT", allocationSize = 1)
public class Contract implements Serializable {

    private static final long serialVersionUID = 1L;

    /*
     * General Tab
     */
    @Id
    @Column(name = "CONTRACT_ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ContractSeq")
    private Integer id;

    /*
     * Customer Company Info
     */

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CUSTOMER_COMPANY_ID", nullable = true ,insertable = false,updatable = false)
    @ForeignKey(name = "FK_CONTRACT_CUSTOMER_COMPANY_ID")
    private Company customerCompany;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "VENDOR_COMPANY_ID", nullable = true ,insertable = false,updatable = false)
    @ForeignKey(name = "FK_CONTRACT_CVENDOR_COMPANY_ID")
    private Company vendorCompany;

    //What mapping should i do to get CUSTOMER_COMPANY_ID Timeline list here


    //What mapping should i do to get vendor_COMPANY_ID Timeline list here

}

Company.java

@Entity
@Table(name = "COMPANY")
@SequenceGenerator(name="CompanySeq", sequenceName="SEQ_COMPANY", allocationSize=1)
public class Company implements Serializable {

    private static final long serialVersionUID = 1L;

    /*
     * Customer Company Details 
     */

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CompanySeq")
    @Column(name = "COMPANY_ID")
    private Integer id;

    @Column(name="CREATION_DATE")
    @Temporal(value=TemporalType.TIMESTAMP)
    private Date creationDate;

    @Column(name="CREATED_BY")
    private String createdBy;

    @OneToMany(fetch = FetchType.LAZY, orphanRemoval=true)
    @Cascade({org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    @JoinColumn(name = "COMPANY_ID",nullable=false)
    @OrderBy(clause = "AS_OFF_DATE" )
    @ForeignKey(name = "PF_COMPANY_TIMELINE")     
    private List<CompanyTimeline> companyTimeline = new ArrayList<CompanyTimeline>();
}

CompanyTimeline.java

@Entity
@Table( name = "COMPANY_TIMELINE")
@SequenceGenerator(name = "CompanyTimelineSeq", sequenceName = "COMPANY_TIMELINE_SEQUENCE")
public class CompanyTimeline {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CompanyTimelineSeq")
    @Column(name = "COMPANY_EVENT_ID")
    private Integer eventId;

    @ManyToOne(fetch= FetchType.EAGER)
    @JoinColumn(name = "COMPANY_ID", updatable = false, insertable = false, nullable=false)
    @ForeignKey(name = "FK_COMPANY_ID", inverseName = "COMPANY_ID")
    private Company company;
}

现在我想在合约表中进行映射(一对多)以获取customer_company和vendor_company时间轴。

但由于连接列名称不相同,Contract表的列名为CUSTOMER_COMPANY_ID,VENDOR_COMPANY_ID,但CompanyTimeline类的列名为COMPANY_ID。

我对如何在Contract.java中进行此连接映射感到困惑。

编辑:

我想根据contract.customer_company_id = timeline.company_id将合约表与时间表进行映射。像...这样的东西。

@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name="COMPANY_ID", referencedColumnName="CUSTOMER_COMPANY_ID")
@Where(clause="to_date(SIGNING_DATE, 'DD-Mon-YY') >= (CASE WHEN event_start_date is null THEN to_date('01-JAN-1901', 'DD-Mon-YYYY') else event_start_date END) AND to_date(SIGNING_DATE, 'DD-Mon-YY') <= (CASE WHEN event_end_date is null THEN to_date('01-JAN-2101', 'DD-Mon-YYYY') else event_end_date END)")
private List<CompanyTimelineView> customerCompanyTimelineView = new ArrayList<CompanyTimelineView>();

这就是我想在contract.java中做的事情,这样我就可以直接获得时间轴对象,而不需要急切的公司。

2 个答案:

答案 0 :(得分:7)

您可以指定参考列名称

@JoinColumn(name="userlastname_fk", referencedColumnName="lastName")

参见Hibernate文档[1]第2.2.3.2.1节

[1] http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/

答案 1 :(得分:1)

列的名称无关紧要。合同与供应商公司有关联,与客户公司有关联。该公司与其时间表有关联。所以你不需要任何其他东西。如果您想要合同的供应商公司的时间线,只需致电

contract.getVendorCompany().getTimeLines();

旁注:公司和CompanyTimeLine之间的双向OneToMany是错误的。它应该是:

@OneToMany(fetch = FetchType.LAZY, orphanRemoval=true, mappedBy = "company")
@Cascade({org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
@OrderBy(clause = "AS_OFF_DATE" )
private List<CompanyTimeline> companyTimeline = new ArrayList<CompanyTimeline>();