我有两张桌子
Customer Rate
------------- -----------
res_number product
strategy name
fname type
lname rate
..... ......
我创建了两个豆
1. 2.
@Entity @Entity
@Table(name="customer") @Table(name="rates")
EmployeeDetails{ CardDetails{
@Col(name="res_number") @col(name="product")
String resNum; String product;
..... ....
} }
现在我的查询是
hql = "from CardDetails cd, EmployeeDetails ed where ed.strategy = cd.product".
但它给了我hibernate.QueryException: could not resolve property:
我尝试过添加
@OneToOne(mappedBy = "strategy")
@Cascade(value = CascadeType.ALL)
private EmployeeDetails empDetails;
在CardDetails中的但它给了我一个错误,说没有OneToOne是可能的ani ...尝试改为ManyToOne和OneToMany但不起作用。任何人都可以告诉我如何使用注释映射bean以进行连接?注意:数据库设计不正确,两个表中都没有公共字段(如外键)。所以非常感谢任何帮助。
修改
添加bean:
@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;
答案 0 :(得分:1)
根据您的HQL的外观,您希望使用Customer表中的stratety
和Rate表中的product
加入这两个表。这意味着strategy
是外键。
如果这确实是一对一的关系,那么在CardDetails中,试试这个:
@OneToOne
@JoinColumn(name = "product", referencedColumnName = "strategy")
private EmployeeDetails employeeDetails;
这假设你没有将product
映射为CardDetails中的属性,如果你这样做,你需要这样做,否则Hibernate会抱怨重复的字段映射。
@Column(name = "product", columnDefinition = "char")
private String product;
@OneToOne
@JoinColumn(name = "product", referencedColumnName = "strategy", insertable = false, updatable = false)
private EmployeeDetails employeeDetails;
如果它需要是一对多的关系,那么就这样做:
@Column(name = "product", columnDefinition = "char")
private String product;
@OneToMany
@JoinColumn(name = "product", referencedColumnName = "strategy", insertable = false, updatable = false)
private List<EmployeeDetails> employeeDetails;