如果列名不同,则一对多休眠连接 [英] One to many hibernate join if column names are different

查看:22
本文介绍了如果列名不同,则一对多休眠连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have three tables with following structure -

Contract -> contract_id(primary), customer_company_id, Vendor_company_id

Contract -> contract_id(primary), customer_company_id, Vendor_company_id

公司 -> Company_id(primary), creation_date, created_by

Company -> Company_id(primary), creation_date, created_by

Company_Timeline -> Timeline_id(Primary)、Company_id、Company_name.

Company_Timeline -> Timeline_id(Primary), Company_id, Company_name.

并为这些表提供以下类 -

and have following classes for these tables -

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 时间线.

Now i want to do a mapping(One to Many) in contract table to get customer_company and vendor_company timeline.

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

But as join column name are not same, Contract table has column name as CUSTOMER_COMPANY_ID, VENDOR_COMPANY_ID but CompanyTimeline class has column name as COMPANY_ID.

我有点困惑如何在 Contract.java 中创建这个连接映射.

I am bit confused how make this join mapping in Contract.java.

我想根据contract.customer_company_id = timeline.company_id 映射合同表和时间线.有点像..

I want to map contract table with timeline on the basis of contract.customer_company_id = timeline.company_id. Something like..

@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 中做的事情,这样我就可以直接获得时间轴对象,而无需急切的陪伴.

This is what i want to do in contract.java so that i can get timeline object directly without eager company.

推荐答案

可以指定引用列名

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

参见 Hibernate 文档 [1] 2.2.3.2.1 节

see Hibernate documentation [1] section 2.2.3.2.1

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

这篇关于如果列名不同,则一对多休眠连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆