2014-02-07 97 views
0

春天JPA ORM的關係我建立應用程序與Spring + Hibernate的彈簧+ JPA的數據PostgreSQL的+。我有兩個不同資源引用Marital類的Person類。與多個持久化單元

  • 人在數據庫pg_land上有tx_person表。
  • 婚姻有tx_marital表在數據庫pg_master。

我可以檢索到對方當人與婚姻之間沒有關係。當我在兩個類之間建立關係(@ManyToOne和@OneToMany)時發生問題。

測試類

package id.co.p**g.sandbox; 

import org.springframework.context.support.GenericXmlApplicationContext; 
import org.springframework.data.domain.Page;  
import id.co.p**g.land.Person; 
import id.co.p**g.service.land.PersonService; 

public class PersonTest { 

    public static void main(String[] args) { 
     GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); 
     ctx.load("classpath:spring-data-app-context.xml"); 
     ctx.load("classpath:datasource.xml"); 
     ctx.refresh(); 

     PersonService personService = ctx.getBean(
       "springJpaPersonService", PersonService.class); 

     Page<Person> requests = personService.getPerson(1); 
     listSurveys(requests); 

    } 


    private static void listSurveys(Page<Person> requests) { 
     // TODO Auto-generated method stub 
     System.out.println("List of person"); 
     for(Person request: requests) { 
      System.out.println(request); 
     } 
    } 
} 

錯誤消息

Exception in thread "main" org.springframework.data.mapping.PropertyReferenceException: No property no found for type id.co.palmagroup.land.Person 
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:74) 
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:325) 
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:305) 
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:269) 
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:240) 
    at org.springframework.data.jpa.repository.query.QueryUtils.toJpaOrder(QueryUtils.java:427) 
    at org.springframework.data.jpa.repository.query.QueryUtils.toOrders(QueryUtils.java:390) 
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:457) 
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:438) 
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:320) 
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:290) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:358) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:343) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:92) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 
    at com.sun.proxy.$Proxy35.findAll(Unknown Source) 
    at id.co.palmagroup.service.land.PersonImpl.getPerson(PersonImpl.java:63) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 
    at com.sun.proxy.$Proxy39.getPerson(Unknown Source) 
    at id.co.palmagroup.sandbox.PersonTest.main(PersonTest.java:20) 

Person類

package id.co.p**g.land; 

import id.co.p**g.master.Marital; 

import java.util.UUID;  
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.Table; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne;  
import org.hibernate.annotations.GenericGenerator; 
import org.hibernate.annotations.Type; 
import org.hibernate.validator.constraints.NotEmpty; 

@Entity 
@Table(name = "tx_person") 
public class Person { 
    private UUID id; 
    private String name; 
    private Marital marital; 

    @Id 
    @GeneratedValue(generator = "system-uuid") 
    @GenericGenerator(name = "system-uuid", strategy = "uuid2") 
    @Type(type="pg-uuid") 
    @Column(name = "id")  
    public UUID getId() { 
     return id; 
    } 

    public void setId(UUID id) { 
     this.id = id; 
    } 

    @NotEmpty(message="{validation.formcode.NotEmpty.message}") 
    @Column(name = "name") 
    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @ManyToOne(targetEntity=Marital.class) 
    @JoinColumn(name="id_marital") 
    @Type(type="pg-uuid") 
    public Marital getMarital() { 
     return marital; 
    } 

    public void setMarital(Marital marital) { 
     this.marital = marital; 
    } 

    public String toString() { 
     return "Person ID: " + id 
       + ". Name: " + name 
       + ". Marital: " + marital; 
    } 
} 

婚姻類

package id.co.p**g.master; 

import id.co.p**g.land.Person; 

import java.util.Set; 
import java.util.UUID; 
import java.io.Serializable; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 

import org.hibernate.annotations.GenericGenerator; 
import org.hibernate.annotations.Type; 

@Entity 
@Table(name = "m_marital") 
public class Marital implements Serializable { 
    private UUID id; 
    private String name; 
    private String description; 
    private int no; 
    private Set<Person> persons; 

    @Id 
    @GeneratedValue(generator = "system-uuid") 
    @GenericGenerator(name = "system-uuid", strategy = "uuid2") 
    @Type(type="pg-uuid") 
    @Column(name = "id")  
    public UUID getId() { 
     return this.id; 
    } 

    public void setId(UUID id) { 
     this.id = id; 
    } 

    @Column(name = "name") 
    public String getName() { 
     return this.name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @Column(name = "description") 
    public String getDescription() { 
     return this.description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    @OneToMany(targetEntity=Person.class, mappedBy="marital") 
    public Set<Person> getPersons() { 
     return this.persons; 
    } 

    @Column(name="no") 
    public int getNo() { 
     return no; 
    } 

    public void setNo(int no) { 
     this.no = no; 
    } 

    public void setPersons(Set<Person> persons) { 
     this.persons = persons; 
    } 

    public String toString() { 
     return + no + ". " 
       + "Marital ID: " + id 
       + " Name: " + name 
       + ". Description: " + description; 
    } 
} 

人實現類

package id.co.p**g.service.land; 

import java.util.List; 
import java.util.UUID; 

import javax.persistence.EntityManager; 
import javax.persistence.PersistenceContext;  
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.data.domain.Page; 
import org.springframework.data.domain.PageRequest; 
import org.springframework.data.domain.Pageable; 
import org.springframework.data.domain.Sort; 
import org.springframework.stereotype.Repository; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional;  
import com.google.common.collect.Lists;  
import id.co.p**g.land.Person; 
import id.co.p**g.repository.land.PersonRepository; 

@Service("springJpaPersonService") 
@Repository 
@Transactional 
public class PersonImpl implements PersonService { 
    // TODO PAGE_SIZE, rubah menjadi konfigurasi parameter. 
    private static final int PAGE_SIZE = 10; 

    @Autowired 
    private PersonRepository personRepository; 

    @PersistenceContext(unitName="landPersistence") 
    private EntityManager em; 

    @Transactional(readOnly=true) 
    public List<Person> findAll() { 
     return Lists.newArrayList(personRepository.findAll()); 
    } 

    public Person save(Person person) { 
     return personRepository.save(person); 
    } 

    public void delete(Person person) { 
     personRepository.delete(person); 
    } 

    @Transactional(readOnly=true) 
    public Person findById(UUID id) { 
     return personRepository.findById(id); 
    } 


    @Transactional(readOnly=true) 
    public Page<Person> findAllByPage(Pageable pageable) { 
     return personRepository.findAll(pageable); 
    } 

    @Override 
    public Page<Person> getPerson(Integer pagenumber) { 
     PageRequest pagerequest = new PageRequest(pagenumber - 1, PAGE_SIZE, Sort.Direction.ASC, "no"); 
     return personRepository.findAll(pagerequest); 
    } 
} 

婚姻實現類

package id.co.p**g.service.master; 

import java.util.List; 
import java.util.UUID;  
import javax.persistence.EntityManager; 
import javax.persistence.PersistenceContext;  
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.data.domain.Page; 
import org.springframework.data.domain.PageRequest; 
import org.springframework.data.domain.Pageable; 
import org.springframework.data.domain.Sort; 
import org.springframework.stereotype.Repository; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional;  
import com.google.common.collect.Lists;  
import id.co.p**g.master.Marital; 
import id.co.p**g.repository.master.MaritalRepository; 

@Service("springJpaMaritalService") 
@Repository 
@Transactional 
public class MaritalImpl implements MaritalService { 
    // TODO PAGE_SIZE, rubah menjadi konfigurasi parameter. 
    private static final int PAGE_SIZE = 10; 

    @Autowired 
    private MaritalRepository maritalRepository; 

    @PersistenceContext(unitName="masterPersistence") 
    private EntityManager em; 

    @Transactional(readOnly=true) 
    public List<Marital> findAll() { 
     return Lists.newArrayList(maritalRepository.findAll()); 
    } 

    @Transactional(readOnly=true) 
    public Marital findById(UUID id) { 
     return maritalRepository.findById(id); 
    } 


    @Transactional(readOnly=true) 
    public Page<Marital> findAllByPage(Pageable pageable) { 
     return maritalRepository.findAll(pageable); 
    } 

    @Override 
    public Page<Marital> getMarital(Integer pagenumber) { 
     PageRequest pagerequest = new PageRequest(pagenumber - 1, PAGE_SIZE, Sort.Direction.ASC, "name"); 
     return maritalRepository.findAll(pagerequest); 
    } 
} 

應用上下文

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.1.xsd 
     http://www.springframework.org/schema/jdbc 
     http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd 
     http://www.springframework.org/schema/data/jpa 
     http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 

    <!-- LAND --> 
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="emf" /> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager" /> 

    <bean id="emf" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="persistenceUnitName" value="landPersistence"/> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> 
     </property> 
     <property name="packagesToScan" value="id.co.p**g.*" /> 
     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.dialect"> 
        org.hibernate.dialect.PostgreSQLDialect 
       </prop> 
       <prop key="hibernate.max_fetch_depth">3</prop> 
       <prop key="hibernate.jdbc.fetch_size">50</prop> 
       <prop key="hibernate.jdbc.batch_size">10</prop> 
       <prop key="hibernate.show_sql">true</prop> 
      </props> 
     </property> 
    </bean> 

    <!-- MASTER --> 
    <bean id="transactionManagerMaster" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="emfMaster" /> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManagerMaster" /> 

    <bean id="emfMaster" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="persistenceUnitName" value="masterPersistence"/> 
     <property name="dataSource" ref="dataSourceMaster" /> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> 
     </property> 
     <property name="packagesToScan" value="id.co.p**g.*" /> 
     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.dialect"> 
        org.hibernate.dialect.PostgreSQLDialect 
       </prop> 
       <prop key="hibernate.max_fetch_depth">3</prop> 
       <prop key="hibernate.jdbc.fetch_size">50</prop> 
       <prop key="hibernate.jdbc.batch_size">10</prop> 
       <prop key="hibernate.show_sql">true</prop> 
      </props> 
     </property> 
    </bean> 

    <context:annotation-config /> 

    <context:component-scan base-package="id.co.p**g.service.*" /> 

    <jpa:repositories base-package="id.co.p**g.repository.land" 
     entity-manager-factory-ref="emf" transaction-manager-ref="transactionManager" /> 

    <jpa:repositories base-package="id.co.p**g.repository.master" 
     entity-manager-factory-ref="emfMaster" transaction-manager-ref="transactionManagerMaster" /> 

</beans> 

數據源配置文件

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.1.xsd 
     http://www.springframework.org/schema/jdbc 
     http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd 
     http://www.springframework.org/schema/data/jpa 
     http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 

    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="org.postgresql.Driver" /> 
     <property name="url" value="jdbc:postgresql://localhost:5432/pg_land" /> 
     <property name="username" value="sysland" /> 
     <property name="password" value="password" /> 
    </bean> 

    <bean id="dataSourceMaster" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="org.postgresql.Driver" /> 
     <property name="url" value="jdbc:postgresql://localhost:5432/pg_master" /> 
     <property name="username" value="usermaster" /> 
     <property name="password" value="password" /> 
    </bean> 

</beans> 
+1

你不能有不同的持久化單元的實體之間的關聯。就這麼簡單。 –

+0

那麼當應用程序必須訪問2個不同的數據庫時,常見的解決方案是什麼? – csutanto

+0

擁有2個持久性單元,使用沒有跨持久性單元關聯的實體。 –

回答

0

我在不同的數據庫使用不同的聯想。 用@ transient標記關係,並放置實體的id。 當你需要引用另一個數據庫的實體的ID。只使用一個發現

+0

你有關於此的網頁詳細解釋嗎?我用hibernate-entitymanager 3.6.8.Final。 – csutanto

相關問題