hibernate3.0+ejb3 annotaion配置实战+spring1.21 annotation事务控制

我是比较讨厌xml的人,没有强类型,很多配置出错,包括xdoclet都无法检查。刚好现在的主流框架总算开始支持annotation了,所以玩了一下配置,供参考:
hibernate3.05
hibernate-annotations-3.0beta2
spring1.21

几个配置文件如下:
spring-database.xml(这个文件基本不变,有新的dao在底下加入一行即可。
None.gif <? xml version="1.0" encoding="UTF-8" ?>
None.gif
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
None.gif
None.gif
< beans >
None.gif
None.gif    
<!--  ========================= RESOURCE DEFINITIONS =========================  -->
None.gif
None.gif    
< bean  id ="dataSource"  class ="org.apache.commons.dbcp.BasicDataSource"  destroy-method ="close" >
None.gif        
< property  name ="driverClassName" >< value > ${jdbc.driverClassName} </ value ></ property >
None.gif        
< property  name ="url" >< value > ${jdbc.url} </ value ></ property >
None.gif        
< property  name ="username" >< value > ${jdbc.username} </ value ></ property >
None.gif        
< property  name ="password" >< value > ${jdbc.password} </ value ></ property >
None.gif    
</ bean >
None.gif
None.gif    
< bean  id ="sessionFactory"  class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
None.gif        
< property  name ="configurationClass" >< value > org.hibernate.cfg.AnnotationConfiguration </ value ></ property >
None.gif           
< property  name ="dataSource" >< ref  bean ="dataSource" /></ property >
None.gif           
< property  name ="configLocation" >< value > /hibernate.cfg.xml </ value ></ property >
None.gif   
</ bean >
None.gif
None.gif    
< bean  id ="transactionManager"  class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
None.gif           
< property  name ="sessionFactory" >< ref  bean ="sessionFactory" /></ property >
None.gif           
< property  name ="dataSource" >< ref  bean ="dataSource" /></ property >
None.gif    
</ bean >
None.gif
None.gif    
< bean  class ="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
None.gif
None.gif    
< bean  class ="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor" >
None.gif        
< property  name ="transactionInterceptor" >< ref  bean ="transactionInterceptor" /></ property >
None.gif    
</ bean >
None.gif
None.gif    
< bean  id ="transactionInterceptor"  class ="org.springframework.transaction.interceptor.TransactionInterceptor" >
None.gif        
< property  name ="transactionManager" >< ref  bean ="transactionManager" /></ property >
None.gif        
< property  name ="transactionAttributeSource" >
None.gif            
< bean  class ="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource" />
None.gif        
</ property >
None.gif    
</ bean >
None.gif
None.gif    
<!--  ========================= DAO DEFINITIONS =========================  -->
None.gif
None.gif    
< bean  id ="testDao"  class ="org.steeven.TestDaoImpl" >
None.gif         
< property  name ="sessionFactory" >
None.gif            
< ref  bean ="sessionFactory" />
None.gif        
</ property >
None.gif    
</ bean >

None.gif    <!-- ========================= DAO DEFINITIONS ========================= -->
     <bean id="mitService" class="org.steeven.TestServiceImpl"></bean>None.gif
None.gif
</ beans >
None.gif

hibernate.cfg.xml
None.gif <? xml version="1.0" encoding="UTF-8" ?>
None.gif
<! DOCTYPE hibernate-configuration PUBLIC
None.gif        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
None.gif        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>
None.gif
< hibernate-configuration >
None.gif    
< session-factory >
None.gif        
< property  name ="hibernate.hbm2ddl.auto" > create-drop </ property >
None.gif        
< property  name ="hibernate.dialect" > org.hibernate.dialect.MySQLDialect </ property >
None.gif        
< property  name ="hibernate.cglib.use_reflection_optimizer" > true </ property >
None.gif        
< property  name ="default-lazy" > false </ property >
None.gif        
None.gif        
<!--  用于hibernate持久化的对象"  -->
None.gif        
< mapping  class ="org.steeven.Test" />
None.gif        
None.gif          
< listener  type ="pre-update"  class ="org.hibernate.validator.event.ValidatePreUpdateEventListener" />
None.gif          
< listener  type ="pre-insert"  class ="org.hibernate.validator.event.ValidatePreInsertEventListener" />
None.gif    
</ session-factory >
None.gif
</ hibernate-configuration >
None.gif

上面两个就是配置文件的全部了。下面看看代码:
TestDao.java(注意,就是个普通的dao,事务控制由调用者标注)
ExpandedBlockStart.gifContractedBlock.gif public   class  TestDaoImpl extends HibernateDaoSupport implements TestDao  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void save(Testdot.gif data) dot.gif{
InBlock.gif        getHibernateTemplate().saveOrUpdateAll(Arrays.asList(data));
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
Test.java
None.gif @Entity
None.gif@Proxy(lazy 
=   false )
ExpandedBlockStart.gifContractedBlock.gif
public   class  Test  dot.gif {
InBlock.gif    
int id;
InBlock.gif    String name;
InBlock.gif    Map
<String, String> properties;
InBlock.gif    @Column(length 
= 100)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String getName() dot.gif{
InBlock.gif        
return name;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setName(String name) dot.gif{
InBlock.gif        
this.name = name;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    @Id(generate 
= GeneratorType.AUTO)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public int getId() dot.gif{
InBlock.gif        
return id;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setId(int id) dot.gif{
InBlock.gif        
this.id = id;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    @Transient
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public String getNotStoredProp() dot.gif{
InBlock.gif        
return null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    @Type(type 
= "org.steeven.database.MapUserType")
InBlock.gif    @Column(name 
= "properties", length = 1000)
ExpandedSubBlockStart.gif        //Map暂时不被支持,自己持久化到一个大字段
ExpandedSubBlockStart.gif
ContractedSubBlock.gif    
public Map<String, String> getProperties() dot.gif{
InBlock.gif        
return properties;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setProperties(Map<String, String> properties) dot.gif{
InBlock.gif        
this.properties = properties;
ExpandedSubBlockEnd.gif    }

InBlock.gif
}

TestServiceImpl.java(这个类也是从spring中取得,关键就在@Transactional标记)
None.gif @Transactional
ExpandedBlockStart.gifContractedBlock.gif
public   class  TestServiceImpl implements TestService  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void save(Testdot.gif data) dot.gif{
InBlock.gif        TestDao dao 
= (TestDao)ServiceLocator.getInstance().getBean("testDao");
InBlock.gif        dao.save(data[
0]);
InBlock.gif        
if (truethrow new RuntimeException(); //看看数据库是不是什么都没有?!
InBlock.gif
        dao.save(data[1]);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

现在很清爽了,所有的事务控制只需要加几个@Transaction标记。感谢hibernate+spring
另外,hibernate还提供了基于标记的参数校验,请参考下面文档:

参考资源:
spring1.21自带smaples/jpetstore
http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/

初次玩spring&hibernate,请多指教!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值