如何在同一个实体中使用多个@Embedded?

时间:2017-09-18 11:30:06

标签: java hibernate

我在java web应用程序中有一个可嵌入的实体,如下所示:

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

我在另一个实体中使用嵌入字段如下:

@Entity
@Getter
@Setter
public class User {

    @Embedded
    private Address home;

    @Embedded
    private Address work;
}

当我运行应用程序时,发生错误:

  

org.hibernate.MappingException:实体映射中的重复列:   my.package.User列:alley(应该使用insert =" false"   更新="假"。)

我该如何解决?

注意:

  • 我无法使用@AttributeOverrides
  • 我正在使用hibernate 5.2.10。

更新 我使用applicationContext.xml中的配置如下:

<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>

2 个答案:

答案 0 :(得分:0)

您可以使用@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;
}

<强>更新    如果您不想使用@AttributeOverrides,请尝试使用ImplicitNamingStrategyComponentPathImpl.INSTANCE覆盖hibernate命名策略

答案 1 :(得分:0)

我解决了我的问题,我将与你分享答案:

关键是定义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>