How can I depend on annotated sleep mappings from a separate project?

I am trying to figure out how to make comparisons for two different projects that have some entities. Since they only have a limited subset of mappings, my first idea was to split these mappings into a separate jar. I use hibernation annotations to do mappings (so they are in class files, not separate from XML).

Both Project A and Project B depend on this mapping project, which contains only a pair of sleeping mappings. Project A does not have its own mappings, but project B. No matter what I do, this always causes problems, because if I do not set up the persistence block for the mapping project, the mappings will never be accepted. Similarly for project B. If I configure the persistence block in the mapping project, project A works, but running the query in project B just gives me ( Mapping- this is the name of the class):

java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: Mapping is not mapped

I believe this is because project B has its own continuity unit, and obviously the two do not merge. I don’t want this either, I would prefer to only configure it in the A / B project, and not on the bank on which they depend. So, is there a way to tell hibernate to scan and map annotations in the dependency bank and add them to the current continuity block?

+3
source share
1 answer

I do not know if you are using Spring, but I am using Spring features. To get this behavior, using the packagesToScan property among the mappingLocations properties as follows

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingLocations">
        <util:list>
            <value>classpath:br/com/ar/model/repository/hql.ar.hbm.xml</value>
            <value>classpath:br/com/br/model/repository/hql.br.hbm.xml</value>
            <value>classpath:br/com/cr/model/repository/hql.cr.hbm.xml</value>
        </util:list>
    </property>
    <property name="packagesToScan">
        <util:list>
            <value>br.com.ar.model.domain</value>
            <value>br.com.br.model.domain</value>
            <value>br.com.cr.model.domain</value>
        </util:list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
            <prop key="hibernate.connection.charSet">UTF-8</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.validator.autoregister_listeners">false</prop>
        </props>
    </property>
</bean>

Hope this can be helpful

UPDATE

According to JPA specification

, , :

  • XML -
  • jar,
  • , (, exclude-unlisted-classes)

<persistence>
    <persistence-unit name="titan">
        <!--Explicity list of classes-->
        <class>br.com.ar.model.domain.A</class>
        <class>br.com.ar.model.domain.B</class>
        <!--Set up any jar file by using jar-file element-->
        <!--Its value is a path relative to the JAR file that contains persistence.xml-->
        <jar-file>../lib/customer.jar</jar-file>
        <!--ORM mapping file-->
        <!--It may be present anywhere on the class path-->
        <mapping-file>mapping.xml</mapping-file>
        <properties>
            Properties goes here
        </properties>
    </persistence-unit>
</persistence>
+2

Source: https://habr.com/ru/post/1757803/


All Articles