Aaron
Aaron

Reputation: 133

JPA/Hibernate mapping with @SecondaryTable annotation

I have two tables:

part {
    int id; --> primary key, auto generated
    varchar poNo;
    varchar partNo;
    varchar partDesc;
    varchar eccNo;
    ...
}

supplement {
    int id; --> primary key
    varchar poNo; --> foreign key
    varchar partNo; --> foreign key
    varchar venderPartNo;
    varchar exportHSCCode;
    ...
}

I defined one Entity as below:

@Entity
@Table(name="part")
@SecondaryTable(name="supplement", pkJoinColumns ={@PrimaryKeyJoinColumn="id"})
public class Part{
    @Id
    @GeneratedValue
    private Integer id;

    private String poNo;

    private String partNo;

    private String partDesc;

    private String eccNo;
    @Column(table="supplement")
    private String vernderPartNo;
    @Column(table="supplement")
    private String exportHSCCode;
    ...
    getter and setter...
}

Question 1:

when I persist one part, I dob't want to insert one row into supplement table, is there has any configuration or annotation can get it? Because according to above Entity and configuration , when I persist one part, hibernate will generate two insert SQL statement for me:

insert into part(poNo, partNo, partDesc, eccNo) values (?,?,?,?)
insert into supplement(vernderPartNo, exportHSCCode, id) vaules (?,?,?)

which I want is, when persist one part, I didn't set value for any filed of supplement, then just one insert statement: insert into part(poNo, partNo, partDesc, eccNo) values (?,?,?,?)

is it possible?

Question 2:

from the table schema of above, the poNO and partNo is the foreign key, that means , for every related part and supplement, this two field should be has the same value. But I don't know how to map this two column value to supplement table when using the configuration as above.

As for the normal operation for Hibernate, when it process one part, it always generate two insert statement which I mentioned above, and for the secondary table, it's insert statement just contains those fields which has specified it's table name, so for supplement, it's insert statement is :

insert into supplement(vernderPartNo, exportHSCCode, id) vaules (?,?,?)

So, is there has any way to let Hibernate generate the insert statement as below:

insert into supplement(poNo, partNo, vernderPartNo, exportHSCCode, id) vaules (?,?,?,?,?)

Upvotes: 0

Views: 9721

Answers (2)

SSK
SSK

Reputation: 3766

to map two tables with @SecondaryTable

you can use like below on the primary table

@Table(name = "Table_Primary", schema = "schema ")
@SecondaryTable(name = "Table_Secondary", schema = "schema ", pkJoinColumns = {@PrimaryKeyJoinColumn(name = "Table_Primary_Column", referencedColumnName = "Table_Secondary_Column")})

Upvotes: 1

Steve Siebert
Steve Siebert

Reputation: 1894

1) Try the following (I would expect this to work, but Hibernate may not play this way):

@Entity
@Table(name="part")
@SecondaryTable(name="supplement", pkJoinColumns ={@PrimaryKeyJoinColumn="id"})
public class Part{
    @Id
    @GeneratedValue
    private Integer id;

    private String poNo;

    private String partNo;

    private String partDesc;

    private String eccNo;
    @Column(table="supplement")
    @Basic(optional=true)
    private String vernderPartNo;
    @Column(table="supplement")
    @Basic(optional=true)
    private String exportHSCCode;
    ...
    getter and setter...
}

2) You'll need to change how your keying Part. You've defined its primary key as an auto incremented integer...that's gonna be your foreign key in the supplement table. This is actually how JPA wants you to do it (I say that because the JPA specification calls natural keys "legacy"). You have a couple options here:

  1. remove the autoincrement from the Part entity and create a "composite key" class with just the poNo and partNo, and add that class as a field to Part. This will become your Part primary key, and will be the foreign key used in your supplement table.
  2. Forget foreign keys and instead add a @UniqueConstraint to your Part for those two columns (this isn't going to fix your foreign key issue, but it does enforce the constraint you identified)

Upvotes: 0

Related Questions