JPA 定义需要类型转换的字段上的关系

发布于 2024-10-04 15:03:13 字数 348 浏览 1 评论 0原文

我想在“供应商”列上加入两个表, 在发票表中,供应商类型为整数,在供应商表中,供应商类型为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你如我软肋 2024-10-11 15:03:13

据我所知,使用数据透视表(就像表示多对多关系一样)是正确的方法。

像这样的东西应该可以工作:

@Entity
public class Invoice
{
    @JoinTable(name = "invoice_vendor", joinColumns = {
        @JoinColumn(name = "invoice", referencedColumnName = "vendor_id")}, inverseJoinColumns = {
        @JoinColumn(name = "vendor", referencedColumnName = "id")})
    @OneToOne
    private Vendor vendor;
}

其中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:

@Entity
public class Invoice
{
    @JoinTable(name = "invoice_vendor", joinColumns = {
        @JoinColumn(name = "invoice", referencedColumnName = "vendor_id")}, inverseJoinColumns = {
        @JoinColumn(name = "vendor", referencedColumnName = "id")})
    @OneToOne
    private Vendor vendor;
}

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.

她比我温柔 2024-10-11 15:03:13

也许这可以使用瞬态字段来完成

@Entity
public class Employee {
    ...
    private boolean isActive;
    ...
    @Transient
    public boolean getIsActive() {
        return isActive;
    }
    public void setIsActive(boolean isActive) {
        this.isActive = isActive;
    }
    @Basic
    private String getIsActiveValue() {
        if (isActive) {
            return "T";
        } else {
            return "F";
        }
    }
    private void setIsActiveValue(String isActive) {
        this.isActive = "T".equals(isActive);
    }
}

http://en.wikibooks.org/wiki/ Java_Persistence/Basic_Attributes#Conversion

Maybe this can be done using a transient field

@Entity
public class Employee {
    ...
    private boolean isActive;
    ...
    @Transient
    public boolean getIsActive() {
        return isActive;
    }
    public void setIsActive(boolean isActive) {
        this.isActive = isActive;
    }
    @Basic
    private String getIsActiveValue() {
        if (isActive) {
            return "T";
        } else {
            return "F";
        }
    }
    private void setIsActiveValue(String isActive) {
        this.isActive = "T".equals(isActive);
    }
}

http://en.wikibooks.org/wiki/Java_Persistence/Basic_Attributes#Conversion

我的鱼塘能养鲲 2024-10-11 15:03:13

您使用哪个 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文