Java spring,hibernate外键

时间:2014-07-10 00:49:59

标签: java spring hibernate foreign-key-relationship

这种类型的编码新手,因为我正在尝试访问一个表中的值以显示在另一个表中:

Table1: Customer
customer_type

Table2: CustomerType
id   desc
1    poor
2    middle  
3    rich

用户将拥有客户类型(下拉列表)的序列号。我为Customer类提供了这段代码,并在名为CustomerType的类中访问table2,但不确定这是否有效。我是在正确的轨道上吗?

@OneToOne(cascade = CascadeType.ALL, mappedBy = "Customer", fetch =      FetchType.EAGER)
@Column(name = "customer_type", nullable = false)
public CustomerType getcustomerType() {
    return customerType;
}

public void setcustomerType(CustomerType description) {
    this.customerType= description;
}

1 个答案:

答案 0 :(得分:0)

看起来你在正确的轨道上,你需要稍微修改你的表,以使这一切顺利运行。

@OneToOne注释的好例子可以在这里找到:

http://viralpatel.net/blogs/hibernate-one-to-one-mapping-tutorial-using-annotation/