JPA defines a relation for a field that requires type conversion

I want to join the two tables in the "supplier" column. In the invoice table, the supplier type is an integer, in the supplier table the supplier is varchar (10).

Is it possible to do type conversion as well as relate?

@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() { ... }
}
+5
source share
3 answers

As far as I know, using a pivot table (as for a many-to-many relationship) would be the right way.

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

If the invoice_vendor table has an integer identifier in the id column and a varchar link in the vendor_id column.

, ManyToOne , , .

+1

,

@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

0

Which JPA provider are you using?

Hibernate seems to have a separate annotation for this (@JoinColumnsOrFormula). As far as I know, EclipseLink does not offer this annotation.

fooobar.com/questions/571508 / ...

0
source

All Articles