JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Object Relational Mapping » JPA  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
己加入精華區
by koji at 2008-12-16 11:56
本主題所含的標籤
無標籤
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:fjj]
sam_huang74_pc





發文: 13
積分: 0
於 2009-03-08 12:08 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
補充後問題如下請大大們幫忙

persistence.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="lottoJPA">
    <class>
    com.lotto.domain.FactorBingo</class>
    <class>
    com.lotto.domain.FactorBingoPosition</class>
    <class>
    com.lotto.domain.FactorDetail</class>
    <class>
    com.lotto.domain.FactorMaster</class>
  </persistence-unit>
  
  <persistence-unit name="lottoJPA2">
      <class>
      com.lotto.domain.FactorBingo</class>
      <class>
      com.lotto.domain.FactorBingoPosition</class>
      <class>
      com.lotto.domain.FactorDetail</class>
      <class>
      com.lotto.domain.FactorMaster</class>
  </persistence-unit>  
  </persistence>
  


applicationContext.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
    <!-- 指定Properties -->
  <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <list>
        <value>classpath:jdbc.properties</value>
      </list>
    </property>
  </bean>
 
  <!-- Import IOC XML -->
  <import resource="classpath:lotto.xml" />
 
  <bean
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
 
  <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url">
      <value>${datasource.url}</value>
    </property>
    <property name="username" value="${datasource.username}" />
    <property name="password" value="${datasource.password}" />
  </bean>
 
  <bean id="dataSource2"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url">
      <value>${datasource.url2}</value>
    </property>
    <property name="username" value="${datasource.username}" />
    <property name="password" value="${datasource.password}" />
  </bean>
 
  <bean id="emf"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
    <property name="persistenceUnitName" value="lottoJPA" />
    <property name="dataSource" ref="dataSource" />
    <property name="loadTimeWeaver">
      <bean
        class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
    <property name="jpaVendorAdapter">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="MYSQL" />
        <property name="showSql" value="false" />
      </bean>
    </property>
  </bean>
 
  <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="emf" />
  </bean>
 
  <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
      <tx:method name="save*" propagation="REQUIRED" />
      <tx:method name="create*" propagation="REQUIRED" />
      <tx:method name="delete*" propagation="REQUIRED" />
      <tx:method name="*" read-only="true" />
    </tx:attributes>
  </tx:advice>
  <aop:config>
    <aop:pointcut id="aopOperation"
      expression="execution(* com.lotto.service..*.* (..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="aopOperation" />
  </aop:config>
 
    <bean id="emf2"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
        <property name="persistenceUnitName" value="lottoJPA2" />
        <property name="dataSource" ref="dataSource2" />
        <property name="loadTimeWeaver">
            <bean
                class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="MYSQL" />
                <property name="showSql" value="false" />
            </bean>
        </property>
    </bean>
 
    <bean id="txManager2" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="emf2" />
    </bean>
    <tx:advice id="txAdvice2" transaction-manager="txManager2">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="aopOperation2"
            expression="execution(* com.lotto.service2..*.* (..))" />
        <aop:advisor advice-ref="txAdvice2" pointcut-ref="aopOperation2" />
    </aop:config> 
  


我在兩個DAO中分別指定
1
2
3
4
5
6
7
8
9
  @PersistenceUnit(name="lottoJPA")
  public void setEntityManager(EntityManager em) {
      this.entityManager = em;
  }
  @PersistenceUnit(name="lottoJPA2")
  public void setEntityManager(EntityManager em) {
      this.entityManager = em;
  }
  


會出現錯誤
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined

問題
  1. DAO 中使用PersistenceUnit是否正確 ?
  2. 那我要怎樣設定才可以讓EntityManager指定到我的DAO上面 ?
  3. 是不是使用@PersistenceUnit,就要把所有連DB的資料設在persistence.xml,是否還可以在ApplicationContext.xml中指定DataSouce,如下


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
5161 [精華] 只有第一個JpaTransactionManager才可以正確執行 fjj 3689 2008-12-15 17:55
4663 Re:只有第一個JpaTransactionManager才可以正確執行 koji 75 2008-12-15 18:12
4680 Re:只有第一個JpaTransactionManager才可以正確執行 fjj 355 2008-12-15 20:05
4677 Re:只有第一個JpaTransactionManager才可以正確執行 koji 643 2008-12-15 20:58
4639 Re:只有第一個JpaTransactionManager才可以正確執行 fjj 424 2008-12-15 23:23
4611 Re:只有第一個JpaTransactionManager才可以正確執行 koji 245 2008-12-15 23:26
4616 Re:只有第一個JpaTransactionManager才可以正確執行 fjj 277 2008-12-15 23:29
4593 Re:只有第一個JpaTransactionManager才可以正確執行 koji 89 2008-12-15 23:31
4586 Re:只有第一個JpaTransactionManager才可以正確執行 koji 129 2008-12-15 23:56
4610 Re:只有第一個JpaTransactionManager才可以正確執行 fjj 236 2008-12-16 08:22
4648 Re:只有第一個JpaTransactionManager才可以正確執行 koji 605 2008-12-16 10:05
4597 Re:只有第一個JpaTransactionManager才可以正確執行 fjj 669 2008-12-16 10:57
4636 Re:只有第一個JpaTransactionManager才可以正確執行 koji 424 2008-12-16 11:02
4763 Re:只有第一個JpaTransactionManager才可以正確執行 fjj 2952 2008-12-16 11:51
4311 Re:只有第一個JpaTransactionManager才可以正確執行 sam_huang74_pc 3117 2009-03-08 00:44
4287 Re:只有第一個JpaTransactionManager才可以正確執行 fjj 178 2009-03-08 11:25
4403 Re:只有第一個JpaTransactionManager才可以正確執行 sam_huang74_pc 5727 2009-03-08 12:08
4260 Re:只有第一個JpaTransactionManager才可以正確執行 fjj 148 2009-03-08 21:40
4727 Re:只有第一個JpaTransactionManager才可以正確執行 sam_huang74_pc 68 2009-03-09 00:52
» JWorld@TW »  Object Relational Mapping » JPA

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8