简体   繁体   中英

Joining tables in hibernate

I have two tables

Customer                            Rate

    -------------                       -----------
    res_number                          product
    strategy                            name    
    fname                               type
    lname                               rate
    .....                               ......

And I created two beans

1.                                      2. 

    @Entity                                 @Entity
    @Table(name="customer")              @Table(name="rates")
    EmployeeDetails{                                CardDetails{
        @Col(name="res_number")             @col(name="product")
        String resNum;                          String product;
        .....                                       ....    

    }                                       }

Now the query I have is

hql = "from CardDetails cd, EmployeeDetails ed where ed.strategy = cd.product".

But it gives me reference problems saying hibernate.QueryException: could not resolve property:

I have tried adding

@OneToOne(mappedBy = "strategy")
    @Cascade(value = CascadeType.ALL)
    private EmployeeDetails empDetails; 

in CardDetails but it gives me error saying no OneToOne is possible ani... tried changing to ManyToOne and OneToMany but doesnot work. Can anyone please tell me how to map the beans for join using annotations? Note: The database is not designed correctly and there is no common field in both tables(like a foreign key). So any help is greatly appreciated.

EDIT:

Adding the beans:

@Entity
@Table(name="rates")
public class CardDetails {
    @Id
    @Column(name="CARD_NAME")
    String cardName;
    @Column(name="CARD_TYPE")
    String cardType;
    @Column(name="FAQ_PAGE")
    String faqPage;
    @Column(name="GEN_INTRO_DISCL")
    String genIntroDiscl;
    @Column(name="GEN_REGULAR_RATE")
    String genRegularRate;
    @Column(name="BT_FEE")
    String btFee;
    @Column(name="BONUS")
    String bonus;
    @Column(name="ART_WORK")
    String artWork;
    @Column(name="DISCLOSURE_LINK")
    String disclosureLink;
    @Column(name="TERMS_LINK")
    String termsLink;
    @Column(name="PRODUCT")
    String product;

@Entity
@Table(name="CUSTOMER")
@SecondaryTable(name="APPLICANT")
public class EmployeeDetails {
    @Id
    @Column(name="RESERVATION_NUMBER")
    String reservNumber;
    @Column(name="SSN")
    int ssnNumber;
    @Column(name="BANK_NUMBER")
    String bankNumber;
    @Column(name="BRANCH_NUMBER")
    String branchNumber;
    @Column(name="EMPLOYEE_ID")
    String empId;
    @Column(name="STRATEGY")
    String strategy;

From the looks of your HQL, you'd like to join the two tables using stratety in the Customer table and product in the Rate table. This would imply that strategy is a foreign key.

If this is indeed a one-to-one relationship, then inside of CardDetails, try this:

@OneToOne
@JoinColumn(name = "product", referencedColumnName = "strategy")
private EmployeeDetails employeeDetails;

This assumes you don't already have product mapped as a property in CardDetails, if you do, you'll need to do it this way, otherwise Hibernate will complain about duplicate field mappings.

@Column(name = "product", columnDefinition = "char")
private String product;

@OneToOne
@JoinColumn(name = "product", referencedColumnName = "strategy", insertable = false, updatable = false)
private EmployeeDetails employeeDetails;

If it needs to be a one-to-many relationship, then do it this way:

@Column(name = "product", columnDefinition = "char")
private String product;

@OneToMany
@JoinColumn(name = "product", referencedColumnName = "strategy", insertable = false, updatable = false)
private List<EmployeeDetails> employeeDetails;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM