JPA 定义需要类型转换的字段上的关系
我想在“供应商”列上加入两个表, 在发票表中,供应商类型为整数,在供应商表中,供应商类型为 varchar(10)。
是否可以进行类型转换并且也有关系?
@Entity
public class Vendor
{
private String id;
@Id(Column="vendor")
public String getId(){ ... }
}
@Entity
public class Invoice
{
private Vendor vendor;
@One-to-one
public Vendor getVendor() { ... }
}
I want to join two tables on column "vendor",
In invoice table vendor type is integer, in vendor table, vendor is type varchar(10).
Is it possible to do a type conversion and also have a relationship?
@Entity
public class Vendor
{
private String id;
@Id(Column="vendor")
public String getId(){ ... }
}
@Entity
public class Invoice
{
private Vendor vendor;
@One-to-one
public Vendor getVendor() { ... }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,使用数据透视表(就像表示多对多关系一样)是正确的方法。
像这样的东西应该可以工作:
其中invoice_vendor表在id列中有整数id,在vendor_id列中有varchar引用。
另外,我猜想您希望供应商之间存在多对一关系,但您已经编写了一对一的关系,因此我将其保留为原样。
As far as I know, using a pivot table (as you would do to represent a many-to-many relationship) would be the right way to do that.
Something like this should work:
Where the invoice_vendor table has the integer id in id column and varchar reference in vendor_id column.
Also I surmise you would like a ManyToOne relationship between vendors, but you've written one to one, so I have left that as such.
也许这可以使用瞬态字段来完成
http://en.wikibooks.org/wiki/ Java_Persistence/Basic_Attributes#Conversion
Maybe this can be done using a transient field
http://en.wikibooks.org/wiki/Java_Persistence/Basic_Attributes#Conversion
您使用哪个 JPA 提供商?
Hibernate 似乎对此有一个单独的注释(@JoinColumnsOrFormula)。据我所知,EclipseLink 不提供该注释。
查看 stackoverflow 上的相关问题
Which JPA provider do you use?
It seems that Hibernate has a separate annotation for this (@JoinColumnsOrFormula). To my knowledge, EclipseLink does not offer that annotation.
See related question on stackoverflow