首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

开箱即用(配置过程),使用spring 减少配置hibernate 地图ping 的痛苦(AnnotationSessionFactoryBean)

2012-08-29 
开箱即用(配置过程),使用spring 减少配置hibernate mapping 的痛苦(AnnotationSessionFactoryBean)今天在

开箱即用(配置过程),使用spring 减少配置hibernate mapping 的痛苦(AnnotationSessionFactoryBean)

今天在给公司新来的同事讲解项目架构的时候,项目中用到了JPA 正向生成数据库的技术,每新建一个pojo@entity,都需要在 hibernate.cfg.xml 增加一个???? mapping?

下面附上我实践的全过程,(要是不想看我废话的,直接拉到最后看我的配置)

<mapping />

?

?

?

   ... 30 moreCaused by: org.hibernate.MappingException: Unable to load class declared as <mapping name="code"><mapping />

?

?

??

... 76 moreCaused by: java.lang.ClassNotFoundException: com.jumbo.model.**.* at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:169)?

?

?

?

      <mapping package="com.jumbo.model" />
?

?

?

19:46:24,177 INFO  [Configuration] configuring from url: file:/E:/Workspaces/baozun/usercenter/usercenter-frontend/src/main/webapp/WEB-INF/classes/hibernate.cfg.xml19:46:24,364 INFO  [AnnotationConfiguration] Mapping package com.jumbo.model19:46:24,599 INFO  [Configuration] Configured SessionFactory: null 

?

?

?

?

?

/*************************************************************************************/   private String           test;    /** PK */   private Long          id;
?

?

?

??

?

?

?

 <bean id="sessionFactory" ref="dataSource" />      <property name="configLocation">        <value>classpath:hibernate.cfg.xml</value>      </property>       <property name="packagesToScan" value="com.jumbo.model" />       </bean>
?

?

18:20:16,095 INFO [DefaultListableBeanFactory] Overriding bean definition for bean 'sessionFactory': replacing [Generic bean: class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [file:/E:/Workspaces/baozun/usercenter/usercenter-frontend/src/main/webapp/WEB-INF/classes/spring.xml]] with [Generic bean: class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [jar:file:/E:/Workspaces/baozun/usercenter/usercenter-frontend/src/main/webapp/WEB-INF/lib/loxia2-core-2.5.jar!/loxia-hibernate-context.xml]]?

?

?

?

?

      <mapping resource="META-INF/orm-master.xml" />      <mapping resource="META-INF/orm-member.xml" />      <mapping resource="META-INF/orm-sales.xml" />      <mapping resource="META-INF/orm-system.xml" />
?

?

?

   <bean id="sessionFactory" ref="dataSource" />      <property name="configLocation" value="classpath:hibernate.cfg.xml" />      <property name="packagesToScan">        <list>           <value>com.jumbo.model</value>        </list>      </property>      <property name="mappingDirectoryLocations">        <list>           <value>/META-INF</value>        </list>      </property>   </bean>
?

?

?

?

?

??

?

?

?

 public Configuration addDirectory(File dir) throws MappingException {      File[] files = dir.listFiles();      for ( int i = 0; i < files.length ; i++ ) {        if ( files[i].isDirectory() ) {           addDirectory( files[i] );        }        else if ( files[i].getName().endsWith( ".hbm.xml" ) ) {           addFile( files[i] );        }      }      return this;   }
?

?

?

?

   <bean id="sessionFactory" ref="dataSource" />      <property name="configLocation" value="classpath:hibernate.cfg.xml" />      <property name="packagesToScan">        <list>           <value>com.jumbo.model</value>        </list>      </property>      <property name="mappingLocations">        <list>           <value>/META-INF/orm-*.xml</value>        </list>      </property>   </bean>
?

?

?

?

?

?

?

?

@Override   public void add(org.dom4j.Document doc) throws MappingException {      boolean ejb3Xml = "entity-mappings".equals( doc.getRootElement().getName() );      if ( inSecondPass ) {        //if in second pass bypass the queueing, getExtendedQueue reuse this method        if ( !ejb3Xml ) {           super.add( doc );        }      }      else {        if ( !ejb3Xml ) {           final Element hmNode = doc.getRootElement();           Attribute packNode = hmNode.attribute( "package" );           String defaultPackage = packNode != null                 ? packNode.getValue()                 : "";           Set<String> entityNames = new HashSet<String>();           findClassNames( defaultPackage, hmNode, entityNames );           for ( String entity : entityNames ) {              hbmEntities.put( entity, doc );           }           hbmDocuments.add( doc );        }        else {           final MetadataProvider metadataProvider = ( ( MetadataProviderInjector ) reflectionManager ).getMetadataProvider();           JPAMetadataProvider jpaMetadataProvider = ( JPAMetadataProvider ) metadataProvider;           List<String> classnames = jpaMetadataProvider.getXMLContext().addDocument( doc );           for ( String classname : classnames ) {              try {                 annotatedClasses.add( reflectionManager.classForName( classname, this.getClass() ) );              }              catch ( ClassNotFoundException e ) {                 throw new AnnotationException( "Unable to load class defined in XML: " + classname, e );              }           }        }      }   } 
?

?

?

?

?

?

15:23:55,216 INFO  [Environment] Hibernate 3.5.6-Final15:23:55,221 INFO  [Environment] hibernate.properties not found15:23:55,231 INFO  [Environment] Bytecode provider name : javassist15:23:55,237 INFO  [Environment] using JDK 1.4 java.sql.Timestamp handling15:23:55,350 INFO  [Version] Hibernate Commons Annotations 3.2.0.Final15:23:55,367 INFO  [Configuration] configuring from url: file:/E:/Workspaces/baozun/usercenter/usercenter-frontend/src/main/webapp/WEB-INF/classes/hibernate.cfg.xml15:23:55,426 INFO  [Configuration] Configured SessionFactory: null15:28:37,410 INFO  [QueryBinder] Binding Named query: Product.findProductByCode => select product from Product as product where product.code = :code15:28:37,413 INFO  [QueryBinder] Binding Named query: Sku.findSkuByCode => select sku from Sku as sku where sku.code = :code15:28:37,418 INFO  [QueryBinder] Binding Named query: Member.findMemberByAccountAndRegSource => select m from Member as m where m.account = :account and m.regSource = :regSource15:28:37,421 INFO  [QueryBinder] Binding Named query: Rank.findRankByCode => select rank from Rank as rank where rank.code = :code15:28:37,424 INFO  [QueryBinder] Binding Named query: MemberAddress.findListByMember => select ma from MemberAddress as ma where ma.member.id = :memberId order by ma.isDefault desc,ma.id15:28:37,428 INFO  [QueryBinder] Binding Named query: MemberLoginLog.getLastLoginLogTime => select loginLog.loginTime from MemberLoginLog as loginLog where loginLog.memberId = :memberId and loginLog.loginStatus= com.jumbo.model.system.LoginStatus.LOGIN_SUCCESS order by loginLog.loginTime desc limit 1;15:28:37,431 INFO  [QueryBinder] Binding Named query: MemberRank.findMemberRankByMemberIdAndRankTypeId => select memberRank from MemberRank as memberRank where memberRank.member.id=:memberId and memberRank.rankType.id=:rankTypeId15:28:37,434 INFO  [QueryBinder] Binding Named query: MemberRank.findMemberRankByMemberIdAndRankTypeCode => select memberRank from MemberRank as memberRank where memberRank.member.id=:memberId and memberRank.rankType.code=:rankTypeCode15:28:37,437 INFO  [QueryBinder] Binding Named query: Rank.findRankByTotalRankConsumption => select rank from Rank as rank where rank.rankType.code =com.jumbo.model.member.RankType.code_ByTotalConsumption and rank.condition <=:totalRankConsumption order by rank.condition desc15:28:37,442 INFO  [QueryBinder] Binding Named query: PointRule.findPointRuleByCode => select pointRule from PointRule as pointRule where pointRule.code =:code15:28:37,444 INFO  [QueryBinder] Binding Named query: ReturnApplication.findReturnApplicationByCode => select ra from ReturnApplication as ra where ra.code = :code15:28:37,447 INFO  [QueryBinder] Binding Named query: ReturnApplicationLine.findReturnApplicationLineByRefId => select ral from ReturnApplicationLine as ral where ral.refId = :refId15:28:37,452 INFO  [QueryBinder] Binding Named query: ReturnOrder.findReturnOrderByCode => select ro from ReturnOrder as ro where ro.code = :code15:28:37,454 INFO  [QueryBinder] Binding Named query: RoLine.findRoLineByRefId => select rol from RoLine as rol where rol.refId = :refId15:28:37,457 INFO  [QueryBinder] Binding Named query: SalesOrder.getSalesOrderList => select so from SalesOrder as so where so.platfrom = :platfrom and so.userName=:userName15:28:37,460 INFO  [QueryBinder] Binding Named query: SalesOrder.findSalesOrderByCode => select so from SalesOrder as so where so.code = :code15:28:37,464 INFO  [QueryBinder] Binding Named query: SoLine.findSalesOrderLineByRefId => select sol from SoLine as sol where sol.refId = :refId15:28:37,467 INFO  [QueryBinder] Binding Named query: ReturnOrderLog.findReturnOrderLogList => select rol from ReturnOrderLog as rol where rol.salesOrder.id = :soId15:28:37,469 INFO  [QueryBinder] Binding Named query: ApiClient.findApiClientByIdAndSecret => select client from ApiClient as client where client.clientId=:clientId and client.clientSecret=:clientSecret15:28:37,474 INFO  [QueryBinder] Binding Named query: ApiTask.findResultByClient => select task from ApiTask as task where task.clientId=:clientId and task.taskId=:taskId15:28:37,476 INFO  [QueryBinder] Binding Named query: ChooseOption.findOptionListByCategoryCode => select o from ChooseOption as o where o.categoryCode = :categoryCode and o.isAvailable = true order by o.sortNo15:28:37,479 INFO  [QueryBinder] Binding Named query: ChooseOption.findAllOptionListByCategoryCode => select o from ChooseOption as o where o.categoryCode = :categoryCode order by o.sortNo15:28:37,482 INFO  [QueryBinder] Binding Named query: ChooseOption.findByCategoryCodeAndKey => select o from ChooseOption as o where o.categoryCode = :categoryCode and o.optionKey = :key15:28:37,486 INFO  [QueryBinder] Binding Named query: Bulletin.findBulletinList => select o from Bulletin as o where o.status = :status and o.effectiveTime <= :effectiveTime15:28:37,489 INFO  [QueryBinder] Binding Named query: MailSubscription.findByEMail => select m from MailSubscription as m where m.email = :email15:28:37,501 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.master.CategoryPropertyName15:28:37,619 INFO  [EntityBinder] Bind entity com.jumbo.model.master.CategoryPropertyName on table T_MA_CAT_PRO_NAME15:28:37,676 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.master.Product15:28:37,681 INFO  [EntityBinder] Bind entity com.jumbo.model.master.Product on table T_MA_PRODUCT15:28:37,690 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.master.ProductCategory15:28:37,692 INFO  [EntityBinder] Bind entity com.jumbo.model.master.ProductCategory on table T_MA_PRO_CAT15:28:37,703 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.master.Sku15:28:37,706 INFO  [EntityBinder] Bind entity com.jumbo.model.master.Sku on table T_MA_SKU15:28:37,713 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.master.SkuDynamicProperty15:28:37,718 INFO  [EntityBinder] Bind entity com.jumbo.model.master.SkuDynamicProperty on table T_MA_SKU_DYN15:28:37,726 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.member.Area15:28:37,728 INFO  [EntityBinder] Bind entity com.jumbo.model.member.Area on table T_MA_AREA15:28:37,733 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.member.Member15:28:37,737 INFO  [EntityBinder] Bind entity com.jumbo.model.member.Member on table T_MA_MEMBER15:28:37,777 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.member.MemberAddress15:28:37,782 INFO  [EntityBinder] Bind entity com.jumbo.model.member.MemberAddress on table T_MA_MEMBER_ADDRESS15:28:37,792 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.member.MemberRank15:28:37,795 INFO  [EntityBinder] Bind entity com.jumbo.model.member.MemberRank on table T_MA_MEMBER_RANK15:28:37,806 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.member.PointRule15:28:37,809 INFO  [EntityBinder] Bind entity com.jumbo.model.member.PointRule on table T_MA_POINTRULE15:28:37,814 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.member.Rank15:28:37,816 INFO  [EntityBinder] Bind entity com.jumbo.model.member.Rank on table T_MA_RANK15:28:37,827 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.member.RankType15:28:37,830 INFO  [EntityBinder] Bind entity com.jumbo.model.member.RankType on table T_MA_RANKTYPE15:28:37,834 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.misc.Bulletin15:28:37,839 INFO  [EntityBinder] Bind entity com.jumbo.model.misc.Bulletin on table t_misc_bulletin15:28:37,884 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.sales.ReturnApplication15:28:37,888 INFO  [EntityBinder] Bind entity com.jumbo.model.sales.ReturnApplication on table T_SO_RETURN_REQUEST15:28:37,901 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.sales.ReturnApplicationLine15:28:37,904 INFO  [EntityBinder] Bind entity com.jumbo.model.sales.ReturnApplicationLine on table T_SO_RETURN_REQUEST_LINE15:28:37,918 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.sales.ReturnOrder15:28:37,921 INFO  [EntityBinder] Bind entity com.jumbo.model.sales.ReturnOrder on table T_SO_RETURN_ORDER15:28:37,930 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.sales.RoLine15:28:37,935 INFO  [EntityBinder] Bind entity com.jumbo.model.sales.RoLine on table T_SO_RETURN_ORDER_LINE15:28:37,945 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.sales.SalesOrder15:28:37,948 INFO  [EntityBinder] Bind entity com.jumbo.model.sales.SalesOrder on table T_SO_SALES_ORDER15:28:37,969 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.sales.SoLine15:28:37,971 INFO  [EntityBinder] Bind entity com.jumbo.model.sales.SoLine on table T_SO_SO_LINE15:28:37,977 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.sales.SoMember15:28:37,980 INFO  [EntityBinder] Bind entity com.jumbo.model.sales.SoMember on table T_SO_SO_MEMBER15:28:37,992 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.ApiClient15:28:37,995 INFO  [EntityBinder] Bind entity com.jumbo.model.system.ApiClient on table T_SYS_API_CLIENT15:28:37,999 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.ApiTask15:28:38,004 INFO  [EntityBinder] Bind entity com.jumbo.model.system.ApiTask on table T_SYS_API_TASK15:28:38,011 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.authorization.OperationUnit15:28:38,014 INFO  [EntityBinder] Bind entity com.jumbo.model.system.authorization.OperationUnit on table T_AU_OPERATION_UNIT15:28:38,025 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.authorization.OperationUnitType15:28:38,028 INFO  [EntityBinder] Bind entity com.jumbo.model.system.authorization.OperationUnitType on table T_AU_OPERATION_UNIT_TYPE15:28:38,033 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.authorization.Privilege15:28:38,036 INFO  [EntityBinder] Bind entity com.jumbo.model.system.authorization.Privilege on table T_AU_PRIVILEGE15:28:38,047 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.authorization.Role15:28:38,050 INFO  [EntityBinder] Bind entity com.jumbo.model.system.authorization.Role on table T_AU_ROLE15:28:38,057 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.authorization.User15:28:38,062 INFO  [EntityBinder] Bind entity com.jumbo.model.system.authorization.User on table T_AU_USER15:28:38,071 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.authorization.UserGroup15:28:38,074 INFO  [EntityBinder] Bind entity com.jumbo.model.system.authorization.UserGroup on table T_AU_USER_GROUP15:28:38,083 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.authorization.UserGroupRelation15:28:38,086 INFO  [EntityBinder] Bind entity com.jumbo.model.system.authorization.UserGroupRelation on table T_AU_USER_GROUP_RELATION15:28:38,090 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.authorization.UserRole15:28:38,092 INFO  [EntityBinder] Bind entity com.jumbo.model.system.authorization.UserRole on table T_AU_USER_ROLE15:28:38,102 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.ChooseOption15:28:38,105 INFO  [EntityBinder] Bind entity com.jumbo.model.system.ChooseOption on table T_SYS_CHOOSE_OPTION15:28:38,109 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.MailSubscription15:28:38,114 INFO  [EntityBinder] Bind entity com.jumbo.model.system.MailSubscription on table T_SYS_MAIL_SUBSCRIPTION15:28:38,122 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.MemberLoginLog15:28:38,125 INFO  [EntityBinder] Bind entity com.jumbo.model.system.MemberLoginLog on table T_SYS_MEM_LOGIN_LOG15:28:38,139 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.MemberMessage15:28:38,142 INFO  [EntityBinder] Bind entity com.jumbo.model.system.MemberMessage on table T_SYS_MEMBER_MSG15:28:38,146 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.MemberPointLog15:28:38,149 INFO  [EntityBinder] Bind entity com.jumbo.model.system.MemberPointLog on table T_SYS_MEMBER_POINT_LOG15:28:38,159 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.MemberRankLog15:28:38,162 INFO  [EntityBinder] Bind entity com.jumbo.model.system.MemberRankLog on table T_SYS_MEMBER_RANK_LOG15:28:38,168 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.ReturnOrderLog15:28:38,172 INFO  [EntityBinder] Bind entity com.jumbo.model.system.ReturnOrderLog on table T_SYS_RETURN_ORDER_LOG15:28:38,180 INFO  [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.SystemMessage15:28:38,183 INFO  [EntityBinder] Bind entity com.jumbo.model.system.SystemMessage on table T_SYS_MESSAGE15:28:38,222 INFO  [CollectionBinder] Mapping collection: com.jumbo.model.member.Member.memberAddresses -> T_MA_MEMBER_ADDRESS15:28:38,225 INFO  [CollectionBinder] Mapping collection: com.jumbo.model.member.Member.memberRanks -> T_MA_MEMBER_RANK15:28:38,228 INFO  [CollectionBinder] Mapping collection: com.jumbo.model.sales.ReturnApplication.appLines -> T_SO_RETURN_REQUEST_LINE15:28:38,234 INFO  [CollectionBinder] Mapping collection: com.jumbo.model.sales.ReturnOrder.roLines -> T_SO_RETURN_ORDER_LINE15:28:38,236 INFO  [CollectionBinder] Mapping collection: com.jumbo.model.sales.SalesOrder.soLines -> T_SO_SO_LINE15:28:38,239 INFO  [CollectionBinder] Mapping collection: com.jumbo.model.system.authorization.OperationUnit.childrenUnits -> T_AU_OPERATION_UNIT15:28:38,244 INFO  [CollectionBinder] Mapping collection: com.jumbo.model.system.authorization.OperationUnitType.ous -> T_AU_OPERATION_UNIT15:28:38,254 INFO  [AnnotationConfiguration] Hibernate Validator not found: ignoring  
?

?

??

<property name="show_sql">false</property> <property name="format_sql">false</property> <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property> <property name="hibernate.query.substitutions">true 1, false 0, yes 'Y', no 'N'</property> <property name="hibernate.jdbc.batch_size">100</property> </session-factory></hibernate-configuration>?

spring.xml 里面 配置

?

?

?

<!--over write sessionFactory in loxia-hibernate-context.xml-->   <bean id="sessionFactory" ref="dataSource" />      <property name="configLocation" value="classpath:hibernate.cfg.xml" />      <property name="packagesToScan">        <list>           <value>com.jumbo.model</value>        </list>      </property>      <property name="mappingLocations">        <list>           <value>/META-INF/orm-*.xml</value>        </list>      </property>   </bean>
?

?

?

?

参考资料:http://stackoverflow.com/questions/1413190/hibernate-mapping-package

?

热点排行