Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How use multiple @Embedded in same entity?

Tags:

java

hibernate

I have an embeddable entity in a java web app as follow:

@Embeddable
@Getter
@Setter
public class Address {
   private String street;
   private String alley;
   private int postCode;
}

I use a embedded field in another entity as follow:

@Entity
@Getter
@Setter
public class User {

    @Embedded
    private Address home;

    @Embedded
    private Address work;
}

When I run application, Occur error :

org.hibernate.MappingException: Repeated column in mapping for entity: my.package.User column: alley(should be mapped with insert="false" update="false").

How can I fix it?

note:

  • I can't use @AttributeOverrides.
  • I'm using hibernate 5.2.10.

update: I use configs in applicationContext.xml as follow:

<bean id="mainSessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="packagesToScan"> 
        <list> 
            <value>my.package</value> 
        </list> 
    </property> 

    <property name="hibernateProperties"> 
        <props> 
            <prop key="hibernate.dialect">org.hibernate.spatial.dialect.postgis.PostgisDialect</prop> 
            <prop key="hibernate.hbm2ddl.auto">update</prop> 
            <prop key="hibernate.connection.characterEncoding">UTF-8</prop> 
            <prop key="hibernate.connection.charSet">UTF-8</prop> 
            <prop key="hibernate.default_schema">public</prop> 
            <prop key="hibernate.implicit_naming_strategy">org.hibernate.boot.‌?model.naming.Impli‌?ci‌?tNamingStrategyCompo‌nentPathImpl</prop> 
        </props> 
    </property> 
</bean> 

<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="mainSessionFactory"/> 
</bean>
like image 315
Morteza Malvandi Avatar asked Sep 18 '17 11:09

Morteza Malvandi


2 Answers

You can achieve using @AttributeOverrides

@Embeddable
@Getter
@Setter
public class Address {
   private String street;
   private String alley;
   private int postCode;
}



@Entity
@Getter
@Setter
public class User {

    @Embedded
    @AttributeOverrides({
       @AttributeOverride(name="street",column=@Column(name="home_street")),
       @AttributeOverride(name="alley",column=@Column(name="home_alley")),
       @AttributeOverride(name="postCode",column=@Column(name="home_postCode"))
    })
    private Address home;

    @Embedded
    @AttributeOverrides({
       @AttributeOverride(name="street",column = @Column(name="work_street")),
       @AttributeOverride(name="alley",column=@Column(name="work_alley")),
       @AttributeOverride(name="postCode",column=@Column(name="work_postCode"))
    })
    private Address work;
}

UPDATE: If you don't want to use @AttributeOverrides then try overring hibernate naming strategy using ImplicitNamingStrategyComponentPathImpl.INSTANCE

like image 121
wolverine Avatar answered Nov 06 '22 05:11

wolverine


I solve my problem and I want to share the answer:

The key point is defining implicitNamingStrategy:

<bean id="mfNamingStrategy"
class="org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl" />
<bean id="mainSessionFactory"
   class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 
    <property name="implicitNamingStrategy" ref="mfNamingStrategy" />
    <property name="hibernateProperties"> 
        <props> 
            ...
            <!-- <prop key="hibernate.implicit_naming_strategy">org.hibernate.boot.‌?model.naming.Impli‌?ci‌?tNamingStrategyCompo‌nentPathImpl</prop> -->
        </props> 
    </property> 
</bean> 
like image 30
Morteza Malvandi Avatar answered Nov 06 '22 04:11

Morteza Malvandi