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

» JWorld@TW » Object Relational Mapping » JPA  

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





發文: 127
積分: 5
於 2008-12-15 17:55 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
各位前輩:

小弟使用 Hibernate JPA + Spring , 欲連結兩個 資料庫, 分別在 persistence.xml 及 applicationContext.xml 中設定 如下:

在 persistence.xml 中設定
1
2
3
4
5
6
7
8
9
10
11
12
  <persistence-unit name="PU1" transaction-type="RESOURCE_LOCAL">
    <non-jta-data-source>jdbc/mysqlDS1</non-jta-data-source>
    <class>test.model.Class1</class>
    <class>...</class>
    <properties/>
  </persistence-unit>
  <persistence-unit name="PU2" transaction-type="RESOURCE_LOCAL">
    <non-jta-data-source>jdbc/mysqlDS2</non-jta-data-source>
    <class>test.model.Class2</class>
    <class>...</class>
    <properties/>
  </persistence-unit>


在 Spring 的 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
    <bean id="emf1" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
        <property name="persistenceUnitName" value="PU1" />
        <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="emf2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence2.xml" />
        <property name="persistenceUnitName" value="PU2" />
        <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="txManager1" class="org.springframework.orm.jpa.JpaTransactionManager" >
        <property name="entityManagerFactory" ref="emf1" />
    </bean>    
    <tx:annotation-driven transaction-manager="txManager1" />
    <tx:advice id="txAdvice" transaction-manager="txManager1">
        <tx:attributes>
            <tx:method name="increasePrice*" propagation="REQUIRED"/>
            <tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW"/>
            <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>
 
    <bean id="txManager2" class="org.springframework.orm.jpa.JpaTransactionManager" >
        <property name="entityManagerFactory" ref="emf2 />
    </bean>
    <tx:annotation-driven transaction-manager="txManager2" />
    <tx:advice id="txAdvice2" transaction-manager="txManager2">
        <tx:attributes>
            <tx:method name="increasePrice*" propagation="REQUIRED"/>
            <tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW"/>
            <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>
 


欲連結兩個資料庫, 各自都有一套 entityManager, 以及 JpaTransation
但是很詭異的是, 資料可以正常撈出來,
但是, persist(), 和 merge() ( 新增和修改 ) 卻只有 第一個 JpaTransactionManager 才可以正確執行,
第二個 JpaTransactionManager 執行了 persist(), 和 merge() 之後, 資料卻都沒有更動也沒有新增!!
Console 下並未看到任何錯誤!!...

因此求教各位前輩, 這樣的方式連結 兩個 JpaTransation , 不知是否有前輩成功過??

或是 有其他的方式可以利用 JPA 連結兩個資料庫??!!
或是小弟上述的設定方式, 是不妥的??!!

先感謝各位前輩!!...


reply to postreply to post
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:fjj]
koji

秒速5センチメートル

站長

發文: 8415
積分: 19
於 2008-12-15 18:12 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
show sql設成true, 那第二個PU2的更動會印出sql嗎?

另外PU2更新時,他在PU1的transaction下嗎?

koji


koji edited on 2008-12-15 18:17
reply to postreply to post
JCConf Taiwan 2015 開始售票了!!
Facebook上的TWJUG社團,歡迎加入
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:fjj]
fjj





發文: 127
積分: 5
於 2008-12-15 20:05 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
嗚嗚....實在是太感動了...感謝前輩願意回覆!!...

1. 不會印出 SQL, 但我在 em.persist(); 的上一行和下一行 我都插了
System.out.println("xxx");
的確是會印出這些標籤!!....

2. 前輩, 這點我無法確定PU2更新時是否在 PU1 的 Transation 下!!....
或是請前輩指導我怎樣可以得到這部分 Information !!....Orz
我實在很想懷疑這個不Work 的原因, 是因為 JpaTransaction 只能起來一個 ,
也就是Spring 的 Singleton,
但是, 我曾經設定過 scope="prototype", 一樣毫無作用....


reply to postreply to post
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:fjj]
koji

秒速5センチメートル

站長

發文: 8415
積分: 19
於 2008-12-15 20:58 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
http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/orm/jpa/JpaTransactionManager.html

This transaction manager is appropriate for applications that use a single JPA EntityManagerFactory for transactional data access. JTA (usually through JtaTransactionManager) is necessary for accessing multiple transactional resources within the same transaction. Note that you need to configure your JPA provider accordingly in order to make it participate in JTA transactions.

所以不知道是不是這原因
看來你是有可能
accessing multiple transactional resources within the same transaction. !?

如果分開叫就可以嗎?
我沒實際碰過multiple 狀況

koji


reply to postreply to post
JCConf Taiwan 2015 開始售票了!!
Facebook上的TWJUG社團,歡迎加入
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:fjj]
fjj





發文: 127
積分: 5
於 2008-12-15 23:23 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
這個小弟有看過, 因此我也曾經改成 JTA 方式, 利用 JBoss 的 JTA 來完成 transation,
但是 JTA 和 JPA 的 Transation 畢竟不一樣,
JPA 的 Transation 是每個persistence Unit 都一個 Transation,
但是 JTA 則是對整個 Application 而言....( 我不很確定我的觀念對不對!!... )
無論如何, 總是沒能讓 PU2 正確的執行新增刪除....

另外, 前輩您所提到的
"如果分開就可以?" <---這個的意思是??
如果您是指分開在不同的 Application ...那是可以Work的 ...

喔對了....我去 Spring Forum 看了一堆 multiple JPA Transation 的討論文...
我發現, 很多人有類似問題, 但好像大家都無解 !!?? ( 不知道我有沒有漏看...)


fjj edited on 2008-12-15 23:26
reply to postreply to post
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:fjj]
koji

秒速5センチメートル

站長

發文: 8415
積分: 19
於 2008-12-15 23:26 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
我是說你的service method,就是套transaction的method
如果每個method內都分開各自操作PU1或PU2
沒有混用兩個PU在同個transaction內,會可以嗎?

另外,所以你不希望都會透過同一個transaction,而是要分開各自的transaction?
但是你都套tx:advice的設定內容都一樣,這樣感覺會不會有點怪
你想要一個method套兩個transaction這樣?, 這不知道spring這樣可不可以哩

koji


koji edited on 2008-12-15 23:30
reply to postreply to post
JCConf Taiwan 2015 開始售票了!!
Facebook上的TWJUG社團,歡迎加入
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:koji]
fjj





發文: 127
積分: 5
於 2008-12-15 23:29 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
koji wrote:
我是說你的service method,就是套transaction的method
如果每個method內都分開各自操作PU1或PU2
沒有混用兩個PU在同個transaction內,會可以嗎?

koji


我都沒有再同一個 method 混用 PU1 和 PU2 ,
甚至連 同一個 Class 都沒有同時出現 PU1& PU2, 都是各自獨立的...

喔對了...我用的都是 @Transational 方式 來註解事務...不知道這樣有沒有關係??!!


fjj edited on 2008-12-15 23:32
reply to postreply to post
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:koji]
koji

秒速5センチメートル

站長

發文: 8415
積分: 19
於 2008-12-15 23:31 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
但是看你設定
tx:advice內容都一樣?
那會操作PU1和PU2的service method名稱改成不同這樣呢?
不然這樣不會變成兩個都套同一個?

koji


reply to postreply to post
JCConf Taiwan 2015 開始售票了!!
Facebook上的TWJUG社團,歡迎加入
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:koji]
koji

秒速5センチメートル

站長

發文: 8415
積分: 19
於 2008-12-15 23:56 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
不知道這算是成功的嗎...0rz..

http://turgayzengin.blogspot.com/2007/09/multiple-persistence-units-with-spring.html
看他也是用jpa...

koji


reply to postreply to post
JCConf Taiwan 2015 開始售票了!!
Facebook上的TWJUG社團,歡迎加入
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:koji]
fjj





發文: 127
積分: 5
於 2008-12-16 08:22 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
koji wrote:
但是看你設定
tx:advice內容都一樣?
那會操作PU1和PU2的service method名稱改成不同這樣呢?
不然這樣不會變成兩個都套同一個?

koji


本來以為已經可以 Work 了, 沒想到是因為我把PU2 設定順序 放前面一點,
如果放後面一點還是不會 work !!...
空歡喜一場!!...Orz

method 改了還是一樣無法 work ....


fjj edited on 2008-12-16 08:30
reply to postreply to post
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:fjj]
koji

秒速5センチメートル

站長

發文: 8415
積分: 19
於 2008-12-16 10:05 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
另外問一下
1
2
3
4
5
6
7
8
<tx:annotation-driven transaction-manager="txManager2" />
    <tx:advice id="txAdvice2" transaction-manager="txManager2">
        <tx:attributes>
            <tx:method name="increasePrice*" propagation="REQUIRED"/>
            <tx:method name="someOtherBusinessMethod" propagation="REQUIRES_NEW"/>
            <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>


都用tx:annotation-driven了
為何還有tx:advice
那你的aop:config有設嗎?

不要用annotation的方式,用xml明確定義去試試看吧

參考看看 http://jira.springframework.org/browse/SPR-3955

koji


koji edited on 2008-12-16 10:08
reply to postreply to post
JCConf Taiwan 2015 開始售票了!!
Facebook上的TWJUG社團,歡迎加入
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:koji]
fjj





發文: 127
積分: 5
於 2008-12-16 10:57 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
koji wrote:
都用tx:annotation-driven了
為何還有tx:advice
那你的aop:config有設嗎?

不要用annotation的方式,用xml明確定義去試試看吧

參考看看 http://jira.springframework.org/browse/SPR-3955


1. 用tx:annotation-driven 的原因是因為我想使用 @Transactional , 所以設定了 tx:annotation-driven

2. 我設定 tx:advice 之後, aop:config 並沒有設定, 我想 aop:config 應該是有預設對應, 應該是不需要才對!!...

3. 我剛剛拿掉了 tx:annotation-driven , 只剩 tx:advice 設定, aop:config 依舊沒設,
service 程式中, 拿掉了 @Transactional , 不過還是不 Work

4. 前輩....在您提供的連結裡面, 我不小心看到了...
With 2.5 (and it turns out also 2.1-m3, 2.1-m4, 2.5.rc-1 and 2.5-rc2) *only* the first tx-annotation-driven element is recognised.
這.....這.....這不會是真的吧!!...Q.Q


fjj edited on 2008-12-16 11:00
reply to postreply to post
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:fjj]
koji

秒速5センチメートル

站長

發文: 8415
積分: 19
於 2008-12-16 11:02 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
1. 所以請不要用@transactional, 他應該會不知道用哪個transaction
2. aop:config沒設定的話,他應該不知道你要對哪些套用才對, 還是你在哪邊有看到不需要設定他也預設會有對應的資訊, 可以提供一下.因為我沒這樣用過.
http://static.springframework.org/spring/docs/2.5.x/reference/transaction.html
我大致看一下沒看到說只要設定tx:xx不需要aop:config..

3. 拿掉tx:annotation-driven , 只剩 tx:advice 設定, aop:config設一下.
記得用到PU1的使用txManager1, PU2的就使用txManager2,這樣應該就可以才對.

4. 所以我叫你先不要用tx:annotation-driven, 請先用明確xml內指定的方式去做做看吧.

koji


koji edited on 2008-12-16 11:07
reply to postreply to post
JCConf Taiwan 2015 開始售票了!!
Facebook上的TWJUG社團,歡迎加入
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:fjj]
fjj





發文: 127
積分: 5
於 2008-12-16 11:51 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
天ㄚ!!....竟然可以了!!....好感動!!....( 流淚激動狀!!.... )
前輩....您....您...您真是佛心來的!!....Orz

整個 在 Spring 的 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
<bean id="emf1" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
    <property name="persistenceUnitName" value="PU1" />
    <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="emf2" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence2.xml" />
    <property name="persistenceUnitName" value="PU2" />
    <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="txManager1" class="org.springframework.orm.jpa.JpaTransactionManager" >
    <property name="entityManagerFactory" ref="emf1" />
</bean>
<!-- DO *NOT* USE <tx:annotation-driven transaction-manager="txManager1" /> -->
<tx:advice id="txAdvice" transaction-manager="txManager1">
    <tx:attributes>
        <tx:method name="save*" propagation="REQUIRED"/>
        <tx:method name="remove*" propagation="REQUIRED"/>
        <tx:method name="*" read-only="true"/>
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:pointcut id="aopOperation1" expression="execution(* com.test.Service1..*.* (..))"/>
    <aop:advisor advice-ref="txAdvice1" pointcut-ref="aopOperation1"/>
</aop:config>
 
<bean id="txManager2" class="org.springframework.orm.jpa.JpaTransactionManager" >
    <property name="entityManagerFactory" ref="emf2 />
</bean>
<!-- DO *NOT* USE <tx:annotation-driven transaction-manager="txManager2" /> -->
<tx:advice id="txAdvice2" transaction-manager="txManager2">
    <tx:attributes>
        <tx:method name="save*" propagation="REQUIRED"/>
        <tx:method name="remove*" propagation="REQUIRED"/>
        <tx:method name="*" read-only="true"/>
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:pointcut id="aopOperation2" expression="execution(* com.test.Service2..*.* (..))"/>
    <aop:advisor advice-ref="txAdvice2" pointcut-ref="aopOperation2"/>
</aop:config>
 


終於可以離開地獄, 進入下一個痛苦的輪迴了!!...Big Smile


fjj edited on 2008-12-16 15:18
reply to postreply to post
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:fjj]
sam_huang74_pc





發文: 13
積分: 0
於 2009-03-08 00:44 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
我的問題跟原問題的需求相同 JPA 要連兩個 DB,設定方式及問題如下

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
  <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
  <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="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> 


1
2
3
4
5
6
7
8
9
  我在兩個DAO中分別指定
  @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

個人認為是因為@PersistenceUnit注入的是EntityManagerFactory但
問題
  1. 那我要怎樣設定才可以讓EntityManager指定到我的DAO上面
  2. 是不是使用@PersistenceUnit,就要把所有連DB的資料設在persistence.xml,是否還可以在ApplicationContext.xml中指定DataSouce,如下


reply to postreply to post
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:fjj]
fjj





發文: 127
積分: 5
於 2009-03-08 11:25 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
恩??!!.....
你所有的設定中都沒看到你的 Database Resource 啊....
那這樣是要怎麼 "注入" ??!!.....你的電腦應該是一頭霧水!!....

你應該在 persistence.xml 或是 applicationContext.xml 中,
指定你的 Database resource 才對!!....


reply to postreply to post
作者 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
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:fjj]
fjj





發文: 127
積分: 5
於 2009-03-08 21:40 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
1,2. DAO 中不需要 setEntityManager , 只需要宣告
private EntityManager em; 就夠了

3. 不論是設在 persistence.xml , 或是 設在ApplicationContext.xml
兩種方法都可以


reply to postreply to post
作者 Re:只有第一個JpaTransactionManager才可以正確執行 [Re:fjj]
sam_huang74_pc





發文: 13
積分: 0
於 2009-03-09 00:52 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
我修正過後還是會出現一樣的錯誤,到Springs也有人提到這個錯誤只是沒有解答,不知道有沒有人可以提供一個完整的Sample供參考,謝謝

reply to postreply to post
» JWorld@TW »  Object Relational Mapping » JPA

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

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8