subbu royal
subbu royal

Reputation: 624

Extra column is adding on db table in hibernate jpa OneToOne relation

I have Three entity class on OneToOne Relation Product->SkuImpl->SkuAvailabilityImpl I just need to add a product, so records are inserting into sku table simultaneously into SkuAvailability table

SkuImpl.java

@Entity 
public class SkuImpl implements  Sku {
    @OneToOne(targetEntity=SkuAvailabilityImpl.class, cascade={cascadeType.ALL},fetch=FetchType.EAGER) 
    @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
    @LazyCollection(LazyCollectionOption.FALSE)
    protected SkuAvailability totalSku;
    //setter and getters
}

SkuAvailabilityImpl.java

@Entity 
public class SkuAvailabilityImpl implements SkuAvailability{
    @OneToOne(optional=true,targetEntity=SkuImpl.class) 
    @Cascade(value={org.hibernate.annotations.CascadeType.ALL})
    @JoinColumn(name="SKU_ID", referencedColumnName='SKU_ID',insertable=false,updatable=false)
    @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
    protected Sku sku;
    //setter and getters
}

sku database table is

SKU_ID  | NAME   | QUANTITY | SKU_AVAILABILITY_ID 
--------+--------+----------+--------------------
        |        |          |

All records are inserting fine, but the problem here is, an extra column is adding on the above table with the name totalSku_SKU_AVAILABILITY_ID like below

SKU_ID  | NAME   | QUANTITY | SKU_AVAILABILITY_ID |totalSku_SKU_AVAILABILITY_ID
--------+--------+----------+--------------------
101     |product1|  200     |                    |  503

I tried deleting that column, but it is creating again. I am not understanding why this column is adding can anyone help me how to stop this?

Upvotes: 3

Views: 885

Answers (1)

Neil Stockton
Neil Stockton

Reputation: 11531

You haven't defined the column name for field "SkuImpl.totalSku" so it defines its own column name according to JPA spec definitions. Add @JoinColumn with the name you have in your database.

Upvotes: 1

Related Questions