2

I am trying to map same embeddable object to my entity class.

     @Entity
    public class Person {
        @Embedded
        public Address home;

        @Embedded
        public Address work;
    }
    @Embeddable
    public class Address {
        public String street;
        public String poBox;

    }

and below is my configuration file

<?xml version="1.0" encoding="UTF-8"?>

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <!-- access -->
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/portfolio" />
    <property name="user" value="root" />
    <property name="password" value="root" />
    <!-- pool sizing -->
    <property name="initialPoolSize" value="3" />
    <property name="minPoolSize" value="6" />
    <property name="maxPoolSize" value="25" />
    <property name="acquireIncrement" value="3" />
    <property name="maxStatements" value="0" />
    <!-- retries -->
    <property name="acquireRetryAttempts" value="30" />
    <property name="acquireRetryDelay" value="1000" /> <!-- 1s -->
    <property name="breakAfterAcquireFailure" value="false" />
    <!-- refreshing connections -->
    <property name="maxIdleTime" value="180" /> <!-- 3min -->
    <property name="maxConnectionAge" value="10" /> <!-- 1h -->
    <!-- timeouts and testing -->
    <property name="checkoutTimeout" value="5000" /> <!-- 5s -->
    <property name="idleConnectionTestPeriod" value="60" /> <!-- 60 -->
    <property name="testConnectionOnCheckout" value="true" />
    <property name="preferredTestQuery" value="SELECT 1" />
    <property name="testConnectionOnCheckin" value="true" />
</bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
        <list>
            <value>com.app.db.entities</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory
            </prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.jdbc.batch_size">20</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.connection.isolation">2</prop>
            ***<prop key="hibernate.implicit_naming_strategy">org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl</prop>***
        </props>
    </property>
</bean>
<bean id="transactionManager"
    class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

On startup its still giving me the below error

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/hibernate-config.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.app.db.entities.Person column: street (should be mapped with insert="false" update="false")

As you can see I am using <prop key="hibernate.implicit_naming_strategy">org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl</prop> in the configuration . I thought this way you can have the embeddable object more than once in a class.

If i give a random string for the naming strategy its failing saying the class is not found. So I assume the configuration is read. Is there something I am missing here ? Any help is appreciated.

Freaky Thommi
  • 756
  • 7
  • 18
  • it does tell you what you have to do – XtremeBaumer Aug 04 '17 at 07:17
  • I have used org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl . So isnt it supposed to take care of the naming ? – Freaky Thommi Aug 04 '17 at 07:19
  • 1
    https://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/chapters/domain/embeddables.html read it carefully – XtremeBaumer Aug 04 '17 at 07:39
  • I saw this. MetadataSources sources = ...; sources.addAnnotatedClass( Address.class ); sources.addAnnotatedClass( Name.class ); sources.addAnnotatedClass( Contact.class ); Metadata metadata = sources.getMetadataBuilder().applyImplicitNamingStrategy( ImplicitNamingStrategyComponentPathImpl.INSTANCE ) ... .build(); But If configure it in the xml file. wont it be taken care for al the entities. I might be being dumb here . Apologies for my ignorance – Freaky Thommi Aug 04 '17 at 09:05
  • have you tried different naming strategies? `org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl` `org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl` `org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl` `org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl` – XtremeBaumer Aug 04 '17 at 09:16
  • @FreakyThommi Did you solve the question? – Morteza Malvandi Sep 19 '17 at 05:00
  • @FreakyThommi I solved the problem at [here](https://stackoverflow.com/a/46292698/3872648) – Morteza Malvandi Sep 19 '17 at 05:51

0 Answers0