Mix JPA Annotations and XML Configuration

I have a rather large (new) project in which we annotated many domain classes with JPA mappings. Now it's time to implement many named queries - some objects can have up to 15-20 named queries. I think that writing these named queries in the annotation will clutter up the source files, and therefore I am considering placing them in XML mapping files.

Is it possible?

Mort is important, is it reasonable?

Are there any better approaches?

How it's done?

+3
source share
3 answers

Is it possible?

Yes, it is, but the trend is more concentrated, and not vice versa.

More importantly, is that wise?

. , , , : . ( , ) , Java- ( , IDE xml). , .

?

, - 1.

?

, XML SQL, (, , ). , , .

1 JPA 1.0 co-lead XML- ( XML) ( ) OTN " ". , . , , .

+6

, , , org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean( Spring, ). JPA, , , , Hibernate, JPA, Spring . , Java Persistence, .

, , , , , . POJO , :

package mypackage.domain;
public class Profile {
    private Long id;
    private String friendlyName;
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getFriendlyName() { return friendlyName; }
    public void setFriendlyName(String friendlyName)
        { this.friendlyName = friendlyName; }
}

src/main/java/mypackage/domain/ ( Maven) , XML (Profile.hbm.xml):

<hibernate-mapping package="mypackage.domain" default-access="field">
<class name="Profile" table="Profile">
    <id name="id" column="ID">
        <generator class="native" />
    </id>
    <property name="friendlyName" column="FriendlyName" />
</class>
</hibernate-mapping>

Hibernate 4.0.0.CR3, , Spring ( 3.0.6.RELEASE) JP Hibernate:

<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="SQL_SERVER" />
        <property name="showSql" value="true" />
    </bean>
    </property>
</bean>

META-INF/persistence.xml :

<persistence version="1.0">
    <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>mypackage.domain.Profile</class>
    </persistence-unit>
</persistence>

, JPA, Hibernate, Hibernate , JDBC , .

+2

, , . , , , - . , . xml, , . - xml, . xml, JPA persistence.xml.

+1

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


All Articles