随笔-124  评论-194  文章-0  trackbacks-0

 

JPA标准+HIBERNATE实现+SPINRG揉和

感觉这3个东东比较好,就查例子,及SPRING文档做了以下配置。

类映射就用JPAannotations好,本来transaction也应该用的,但后来发现aop+advise这种定义方式方便,就先用它,随时可以改过来。这些定义都挺简单,看一下SPRING的文档就都清楚了,而且看最新文档还有个好处,知道最新的写法是什么。

transaction定义在MANAGER上,DAO就注入到它里面。

还有一个晕的地方:

Persistence.xml这个文件就算不用也要定义,它是用来放映射的hbm.xml或者是被annotation的类名的,但我发现其实写了也没用,它好像会自动去搜索hbm.xml以及有annotations的类,自动加到EntityManager里。我只有把不用的hbm.xml删除掉它才会不加。这一点相当奇怪,试了八百多次,也没啥心思研究了,删除了事。

<?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:aop="http://www.springframework.org/schema/aop"

       xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="

       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

       <!--    Data Source Setup -->

       <bean id="dataSource"

              class="org.springframework.jdbc.datasource.DriverManagerDataSource">

              <property name="driverClassName">

                     <value>com.mysql.jdbc.Driver</value>

              </property>

              <property name="url">

                     <value>jdbc:mysql://ebnms:3306/uu</value>

              </property>

              <property name="username">

                     <value>root</value>

              </property>

              <property name="password">

                     <value>78317</value>

              </property>

       </bean>

       <bean id="entityManagerFactory"

              class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

              <property name="dataSource" ref="dataSource" />

              <property name="persistenceUnitName" value="ebnms"/>

              <!--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="true" />

                     </bean>

              </property>

       </bean>

       <bean id="txManager"

              class="org.springframework.orm.jpa.JpaTransactionManager">

              <property name="entityManagerFactory"

                     ref="entityManagerFactory" />

       </bean>

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

       <bean

              class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

       <bean id="monitorDao"

              class="com.exchangebit.nms.model.test.MonitorJpaDao">

              <property name="entityManagerFactory"

                     ref="entityManagerFactory" />

       </bean>

       <bean id="monitorManager"

              class="com.exchangebit.nms.model.test.MonitorManager">

              <property name="monitorDao" ref="monitorDao" />

       </bean>

       <aop:config>

              <aop:pointcut id="monitorDaoMethods"

                     expression="execution(* com.exchangebit.nms.model.test.MonitorManager.*(..))" />

              <aop:advisor advice-ref="txAdvice"

                     pointcut-ref="monitorDaoMethods" />

       </aop:config>

       <tx:advice id="txAdvice" transaction-manager="txManager">

              <tx:attributes>

                     <tx:method name="save*" propagation="REQUIRED" />

              </tx:attributes>

       </tx:advice>

</beans>

Persistence.xml内容:

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

       version="1.0">

       <persistence-unit name="ebnms" transaction-type="RESOURCE_LOCAL">

              <!--provider>org.hibernate.ejb.HibernatePersistence</provider-->

      

              <class>

                     com.exchangebit.nms.model.test.Monitor

              </class>

       </persistence-unit>

</persistence>

DAO代码很简单,从JpaDaoSupport派生类来处理,看不见transactions的那些代码,全部在SPRING的定义里放到manager里去了:

publicclass JpaGenericDAO extends JpaDaoSupport {

       protectedfinal Logger logger = LoggerFactory.getLogger(getClass());

       publicvoid create(T entity) {

              Assert.notNull(entity);

              logger.info("saving {} instance", className);

              getJpaTemplate().persist(entity);

              logger.info("save successful");

       }

}

搭建MAVEN2的内网服务器:设置一个目录在WEB服务上可以访问

/etc/apache2/conf.d下新建配置文件,内容如下:

Alias /maven2 /maven2

<Directory /maven2>

        Options +FollowSymLinks Indexes

        AllowOverride None

        order allow,deny

        allow from all

</Directory>

这里关键是Indexes选项表示可以列出目录。

另外,把httpd的运行用户加入这个目录的owner所在的一个组,并且组的读属性是开的。

设置服务器地址:

C:"WINDOWS"system32"drivers"etc"hosts文件加入ebnms指向内网服务器,然后将第一项repo指向它:

              <repository>

                     <id>artifactory</id>

                     <name>artifactory at my server</name>

                     <url>http://ebnms/maven2</url>

                     <snapshots>

                            <enabled>true</enabled>

                            <updatePolicy>never</updatePolicy>

                     </snapshots>

              </repository>

MYSQL可以被外部机器连接

修改CONFIG文件为绑定全网端口:

bind-address            = 0.0.0.0

本机通过ROOT连接上后,执行命令给权限:

GRANT ALL PRIVILEGES ON *.* TO root@192.168.1.99 IDENTIFIED BY '78317';

试登陆:

mysql -u root -h 192.168.1.17 –p

修改密码:
SET PASSWORD FOR root@'172.19.149.52' = PASSWORD('78317');

cannot connect to VM错误

左提示右提示就是这个错,找了半天是Kaspersky防火墙的问题,其实一早我就怀疑是它,但只是停用了试了下,错误依旧,后来试了一天又试回来,发现要把防火墙的过滤级别设置成最低就可以了通了,晕啊晕,还让我怀疑是不是JAR冲突。

posted on 2008-01-28 23:08 我爱佳娃 阅读(6404) 评论(1)  编辑  收藏 所属分类: 服务配置Springmaven

评论:
# re: 最近在用JPA+SPRING+HIBERNATE及MAVEN2中遇到的问题和解决方法做个笔记 2008-06-24 10:32 | yujiaao@msn.com
我想用JPA动态切换数据库,楼主能帮个忙么?  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: