简体   繁体   English

在休眠中联接表

[英]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: 但这给了我参考问题,说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. 在CardDetails中,但是它给我一个错误,提示没有OneToOne可能。。。尝试更改为ManyToOne和OneToMany但不起作用。 Can anyone please tell me how to map the beans for join using annotations? 谁能告诉我如何使用注解映射bean以便联接? 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. 从你的HQL的样子,你想加入使用两个表stratety Customer表和product的费率表。 This would imply that strategy is a foreign key. 这意味着该strategy是外键。

If this is indeed a one-to-one relationship, then inside of CardDetails, try this: 如果这确实是一对一的关系,请在CardDetails中尝试以下操作:

@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. 这是假设您尚未在CardDetails中将product映射为属性,如果这样做,则需要以这种方式进行,否则Hibernate将抱怨重复的字段映射。

@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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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