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:
@AttributeOverrides
. 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?tNamingStrategyComponentPathImpl</prop>
</props>
</property>
</bean>
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="mainSessionFactory"/>
</bean>
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
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?tNamingStrategyComponentPathImpl</prop> -->
</props>
</property>
</bean>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With