views:

129

answers:

2

I've been struggling with this one for quite some time already. It appears a lot less simple than I thought it'd be:

    <join table="COTISATION_SYNCHRO" fetch="join" optional="true">
        <key column="COTISATION_SYNCHRO_COTISATION_ID_FK" on-delete="noaction"/>
        <property name="cotisationCoupon" type="java.lang.Long" update="true" insert="true">
            <column name="COTISATION_COUPON" not-null="true" unique="true"/>
        </property>
        <property name="synchroData" type="com.allence.opcapl.alpha2.common.model.synchro.SynchroDataType">
            <column name="LAST_ACCESS_LOCAL" not-null="true"/>
            <column name="LAST_UPDATE_LOCAL" not-null="true"/>
            <column name="LAST_ACCESS_REMOTE" not-null="true"/>
            <column name="LAST_UPDATE_REMOTE" not-null="true"/>
        </property>
    </join>

This is included in the COTISATION table mapping and uses SynchroDataType, extending Hibernate UserType.

This works really great, but I can't find a way to translate it to proper JPA while keeping the convenience of it.

Does someone has a solution for that kind of one-to-one mapping?

+1  A: 

Look at the @Embedded annotation to solve your non-entity object SynchroDataType and the @SecondaryTable to handle the one-to-one mapping between COTISATION and COTISATION_SYNCHRO.

mtpettyp
A: 

thanks a lot, I got it working. I was focussing on @JoinTable, wrong direction. @secondaryTable did the trick.

here's the solution:

@Entity
@Table(name = "COTISATION")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@SecondaryTable(name = "COTISATION_SYNCHRO", pkJoinColumns = @PrimaryKeyJoinColumn(name = "COTISATION_SYNCHRO_COTISATION_ID_FK"))
public class Cotisation {
...

@Embedded
@AttributeOverrides({
        @AttributeOverride(name = "lastAccessLocal", column = @Column(name = "LAST_ACCESS_LOCAL", table = "COTISATION_SYNCHRO")),
        @AttributeOverride(name = "lastUpdateLocal", column = @Column(name = "LAST_UPDATE_LOCAL", table = "COTISATION_SYNCHRO")),
        @AttributeOverride(name = "lastAccessRemote", column = @Column(name = "LAST_ACCESS_REMOTE", table = "COTISATION_SYNCHRO")),
        @AttributeOverride(name = "lastUpdateRemote", column = @Column(name = "LAST_UPDATE_REMOTE", table = "COTISATION_SYNCHRO"))
})
private SynchroData synchroData;

@Column(name = "COTISATION_COUPON", table = "COTISATION_SYNCHRO", unique = true)
private Long cotisationCoupon;

with SynchroData class annotated with @Embeddable

nodje