今天在给公司新来的同事讲解项目架构的时候,项目中用到了JPA 正向生成数据库的技术,每新建一个pojo @entity,都需要在 hibernate.cfg.xml 增加一个 mapping
<mapping class="com.jumbo.model.system.MailSubscription"/>
有位同事就问,可不可以使用通配符,不用每次配一个,而且以前负责过Nike 商城的发布,经常发现开发工程师没有在项目发布模版文档里面,把新增的 mapping 写上,导致项目发布出现exception
要是真能做到,新增一个entity,删除一个entity,不用修改 hibernate.cfg.xml 就好了 ,
下面附上我实践的全过程,(要是不想看我废话的,直接拉到最后看我的配置)
晚上实践了下 ,
<mapping class="com.jumbo.model.*" />
走,启动
... 30 more
Caused by: org.hibernate.MappingException: Unable to load class declared as <mapping class="com.jumbo.model.*"/> in the configuration:
at org.hibernate.cfg.AnnotationConfiguration.parseMappingElement(AnnotationConfiguration.java:740)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1647)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1626)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:1224)
报错了,
网上搜搜,是不是我通配符配置错了,再来
<mapping class="com.jumbo.model.**.*" />
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
... 76 more
Caused 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)
哥杯具了
看了下dtd,貌似 mapping还有其他属性(package),试试
<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.xml
19:46:24,364 INFO [AnnotationConfiguration] Mapping package com.jumbo.model
19:46:24,599 INFO [Configuration] Configured SessionFactory: null
但是没有以前见到的 表和 entity 一一对应的效果,貌似没有成功,表(member)里面增加个字段(test)试试
/*************************************************************************************/
private String test;
/** PK */
private Long id;
setter,getter 方法
启动,去数据库看看,表没有新增我刚加的test 字段,看来是配置不成功,
去看看 package到底是做什么的,以前没有接触过,接触过最多的就是 反向工程的 resource(xml 配置),以及正向工程的 class(annotition配置),
网上还找不到具体的介绍,都说这个package 没用
哥心说,”没用的话,hibernate 设置这个值干嘛吃的?” 继续找,终于 在 http://www.jetbrains.net/jira/browse/HBR-4 文档里面 ,发现有牛人回复了
This is not a problem or shortcoming of Hibero.
To specify mapping entities using annotations use the tag <mapping class="fqclassname"/>.
啊,原来 package 用在 package 注释级别的
看来我的用法是错的,但是我的entity 总不能去改吧,(再说 这个package 到底怎么用,还不清楚撒)想想有没有其他的办法,
活人怎么能给尿憋死, 国内国外像我这么懒的人肯定有(不想手写那么多的mapping啊 ),
找找资料 ,果然有”前辈”, http://stackoverflow.com/questions/1413190/hibernate-mapping-package
娃哈哈,spring 的AnnotationSessionFactoryBean 有自动扫描(packagesToScan)的功能 ,尝试着用用看
不过 我们的sessionFactory bean 被我们的架构师封装在 loxia jar 里面
那么我就重写一个
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="packagesToScan" value="com.jumbo.model" />
</bean>
启动的时候 会自动replace
看日志
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]]
我的 hibernate配置文件里面 , 还有named query
<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" />
研究加实验下 ,发现了 mappingDirectoryLocations,心想要是只配个 文件夹 META-INF,自动扫描出 下面的orm-*.xml 世界都美妙阿
go,尝试下
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" 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>
发现没有起作用, 敏思苦想,文件夹式配置都不成功
只有去看看源码,一路debug,发现了 hiberntae 这么个method
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;
}
噢,原来 文件名必须.hbm.xml 结尾才加如到 Configuration, .hbm.xml一般都是配置映射关系的,我的只是namedquery 文件 ,那么 mappingDirectoryLocations这东东我们就用不了,再看看 有没有其他的配置方式
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" 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>
这个可以读取到 配置的namedquery了 ,查看源码
@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 );
}
}
}
}
}
恩,是以InputStream 的形式,xml root element 是 entity-mappings文件 ,yes 是我要的 东东 ,
这么配置,我们看看log,是不是和以前差不多
15:23:55,216 INFO [Environment] Hibernate 3.5.6-Final
15:23:55,221 INFO [Environment] hibernate.properties not found
15:23:55,231 INFO [Environment] Bytecode provider name : javassist
15:23:55,237 INFO [Environment] using JDK 1.4 java.sql.Timestamp handling
15:23:55,350 INFO [Version] Hibernate Commons Annotations 3.2.0.Final
15:23:55,367 INFO [Configuration] configuring from url: file:/E:/Workspaces/baozun/usercenter/usercenter-frontend/src/main/webapp/WEB-INF/classes/hibernate.cfg.xml
15:23:55,426 INFO [Configuration] Configured SessionFactory: null
15:28:37,410 INFO [QueryBinder] Binding Named query: Product.findProductByCode => select product from Product as product where product.code = :code
15:28:37,413 INFO [QueryBinder] Binding Named query: Sku.findSkuByCode => select sku from Sku as sku where sku.code = :code
15:28:37,418 INFO [QueryBinder] Binding Named query: Member.findMemberByAccountAndRegSource => select m from Member as m where m.account = :account and m.regSource = :regSource
15:28:37,421 INFO [QueryBinder] Binding Named query: Rank.findRankByCode => select rank from Rank as rank where rank.code = :code
15: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.id
15: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=:rankTypeId
15: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=:rankTypeCode
15: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 desc
15:28:37,442 INFO [QueryBinder] Binding Named query: PointRule.findPointRuleByCode => select pointRule from PointRule as pointRule where pointRule.code =:code
15:28:37,444 INFO [QueryBinder] Binding Named query: ReturnApplication.findReturnApplicationByCode => select ra from ReturnApplication as ra where ra.code = :code
15:28:37,447 INFO [QueryBinder] Binding Named query: ReturnApplicationLine.findReturnApplicationLineByRefId => select ral from ReturnApplicationLine as ral where ral.refId = :refId
15:28:37,452 INFO [QueryBinder] Binding Named query: ReturnOrder.findReturnOrderByCode => select ro from ReturnOrder as ro where ro.code = :code
15:28:37,454 INFO [QueryBinder] Binding Named query: RoLine.findRoLineByRefId => select rol from RoLine as rol where rol.refId = :refId
15:28:37,457 INFO [QueryBinder] Binding Named query: SalesOrder.getSalesOrderList => select so from SalesOrder as so where so.platfrom = :platfrom and so.userName=:userName
15:28:37,460 INFO [QueryBinder] Binding Named query: SalesOrder.findSalesOrderByCode => select so from SalesOrder as so where so.code = :code
15:28:37,464 INFO [QueryBinder] Binding Named query: SoLine.findSalesOrderLineByRefId => select sol from SoLine as sol where sol.refId = :refId
15:28:37,467 INFO [QueryBinder] Binding Named query: ReturnOrderLog.findReturnOrderLogList => select rol from ReturnOrderLog as rol where rol.salesOrder.id = :soId
15:28:37,469 INFO [QueryBinder] Binding Named query: ApiClient.findApiClientByIdAndSecret => select client from ApiClient as client where client.clientId=:clientId and client.clientSecret=:clientSecret
15:28:37,474 INFO [QueryBinder] Binding Named query: ApiTask.findResultByClient => select task from ApiTask as task where task.clientId=:clientId and task.taskId=:taskId
15: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.sortNo
15:28:37,479 INFO [QueryBinder] Binding Named query: ChooseOption.findAllOptionListByCategoryCode => select o from ChooseOption as o where o.categoryCode = :categoryCode order by o.sortNo
15:28:37,482 INFO [QueryBinder] Binding Named query: ChooseOption.findByCategoryCodeAndKey => select o from ChooseOption as o where o.categoryCode = :categoryCode and o.optionKey = :key
15:28:37,486 INFO [QueryBinder] Binding Named query: Bulletin.findBulletinList => select o from Bulletin as o where o.status = :status and o.effectiveTime <= :effectiveTime
15:28:37,489 INFO [QueryBinder] Binding Named query: MailSubscription.findByEMail => select m from MailSubscription as m where m.email = :email
15:28:37,501 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.master.CategoryPropertyName
15:28:37,619 INFO [EntityBinder] Bind entity com.jumbo.model.master.CategoryPropertyName on table T_MA_CAT_PRO_NAME
15:28:37,676 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.master.Product
15:28:37,681 INFO [EntityBinder] Bind entity com.jumbo.model.master.Product on table T_MA_PRODUCT
15:28:37,690 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.master.ProductCategory
15:28:37,692 INFO [EntityBinder] Bind entity com.jumbo.model.master.ProductCategory on table T_MA_PRO_CAT
15:28:37,703 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.master.Sku
15:28:37,706 INFO [EntityBinder] Bind entity com.jumbo.model.master.Sku on table T_MA_SKU
15:28:37,713 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.master.SkuDynamicProperty
15:28:37,718 INFO [EntityBinder] Bind entity com.jumbo.model.master.SkuDynamicProperty on table T_MA_SKU_DYN
15:28:37,726 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.member.Area
15:28:37,728 INFO [EntityBinder] Bind entity com.jumbo.model.member.Area on table T_MA_AREA
15:28:37,733 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.member.Member
15:28:37,737 INFO [EntityBinder] Bind entity com.jumbo.model.member.Member on table T_MA_MEMBER
15:28:37,777 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.member.MemberAddress
15:28:37,782 INFO [EntityBinder] Bind entity com.jumbo.model.member.MemberAddress on table T_MA_MEMBER_ADDRESS
15:28:37,792 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.member.MemberRank
15:28:37,795 INFO [EntityBinder] Bind entity com.jumbo.model.member.MemberRank on table T_MA_MEMBER_RANK
15:28:37,806 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.member.PointRule
15:28:37,809 INFO [EntityBinder] Bind entity com.jumbo.model.member.PointRule on table T_MA_POINTRULE
15:28:37,814 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.member.Rank
15:28:37,816 INFO [EntityBinder] Bind entity com.jumbo.model.member.Rank on table T_MA_RANK
15:28:37,827 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.member.RankType
15:28:37,830 INFO [EntityBinder] Bind entity com.jumbo.model.member.RankType on table T_MA_RANKTYPE
15:28:37,834 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.misc.Bulletin
15:28:37,839 INFO [EntityBinder] Bind entity com.jumbo.model.misc.Bulletin on table t_misc_bulletin
15:28:37,884 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.sales.ReturnApplication
15:28:37,888 INFO [EntityBinder] Bind entity com.jumbo.model.sales.ReturnApplication on table T_SO_RETURN_REQUEST
15:28:37,901 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.sales.ReturnApplicationLine
15:28:37,904 INFO [EntityBinder] Bind entity com.jumbo.model.sales.ReturnApplicationLine on table T_SO_RETURN_REQUEST_LINE
15:28:37,918 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.sales.ReturnOrder
15:28:37,921 INFO [EntityBinder] Bind entity com.jumbo.model.sales.ReturnOrder on table T_SO_RETURN_ORDER
15:28:37,930 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.sales.RoLine
15:28:37,935 INFO [EntityBinder] Bind entity com.jumbo.model.sales.RoLine on table T_SO_RETURN_ORDER_LINE
15:28:37,945 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.sales.SalesOrder
15:28:37,948 INFO [EntityBinder] Bind entity com.jumbo.model.sales.SalesOrder on table T_SO_SALES_ORDER
15:28:37,969 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.sales.SoLine
15:28:37,971 INFO [EntityBinder] Bind entity com.jumbo.model.sales.SoLine on table T_SO_SO_LINE
15:28:37,977 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.sales.SoMember
15:28:37,980 INFO [EntityBinder] Bind entity com.jumbo.model.sales.SoMember on table T_SO_SO_MEMBER
15:28:37,992 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.ApiClient
15:28:37,995 INFO [EntityBinder] Bind entity com.jumbo.model.system.ApiClient on table T_SYS_API_CLIENT
15:28:37,999 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.ApiTask
15:28:38,004 INFO [EntityBinder] Bind entity com.jumbo.model.system.ApiTask on table T_SYS_API_TASK
15:28:38,011 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.authorization.OperationUnit
15:28:38,014 INFO [EntityBinder] Bind entity com.jumbo.model.system.authorization.OperationUnit on table T_AU_OPERATION_UNIT
15:28:38,025 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.authorization.OperationUnitType
15:28:38,028 INFO [EntityBinder] Bind entity com.jumbo.model.system.authorization.OperationUnitType on table T_AU_OPERATION_UNIT_TYPE
15:28:38,033 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.authorization.Privilege
15:28:38,036 INFO [EntityBinder] Bind entity com.jumbo.model.system.authorization.Privilege on table T_AU_PRIVILEGE
15:28:38,047 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.authorization.Role
15:28:38,050 INFO [EntityBinder] Bind entity com.jumbo.model.system.authorization.Role on table T_AU_ROLE
15:28:38,057 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.authorization.User
15:28:38,062 INFO [EntityBinder] Bind entity com.jumbo.model.system.authorization.User on table T_AU_USER
15:28:38,071 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.authorization.UserGroup
15:28:38,074 INFO [EntityBinder] Bind entity com.jumbo.model.system.authorization.UserGroup on table T_AU_USER_GROUP
15:28:38,083 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.authorization.UserGroupRelation
15:28:38,086 INFO [EntityBinder] Bind entity com.jumbo.model.system.authorization.UserGroupRelation on table T_AU_USER_GROUP_RELATION
15:28:38,090 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.authorization.UserRole
15:28:38,092 INFO [EntityBinder] Bind entity com.jumbo.model.system.authorization.UserRole on table T_AU_USER_ROLE
15:28:38,102 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.ChooseOption
15:28:38,105 INFO [EntityBinder] Bind entity com.jumbo.model.system.ChooseOption on table T_SYS_CHOOSE_OPTION
15:28:38,109 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.MailSubscription
15:28:38,114 INFO [EntityBinder] Bind entity com.jumbo.model.system.MailSubscription on table T_SYS_MAIL_SUBSCRIPTION
15:28:38,122 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.MemberLoginLog
15:28:38,125 INFO [EntityBinder] Bind entity com.jumbo.model.system.MemberLoginLog on table T_SYS_MEM_LOGIN_LOG
15:28:38,139 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.MemberMessage
15:28:38,142 INFO [EntityBinder] Bind entity com.jumbo.model.system.MemberMessage on table T_SYS_MEMBER_MSG
15:28:38,146 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.MemberPointLog
15:28:38,149 INFO [EntityBinder] Bind entity com.jumbo.model.system.MemberPointLog on table T_SYS_MEMBER_POINT_LOG
15:28:38,159 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.MemberRankLog
15:28:38,162 INFO [EntityBinder] Bind entity com.jumbo.model.system.MemberRankLog on table T_SYS_MEMBER_RANK_LOG
15:28:38,168 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.ReturnOrderLog
15:28:38,172 INFO [EntityBinder] Bind entity com.jumbo.model.system.ReturnOrderLog on table T_SYS_RETURN_ORDER_LOG
15:28:38,180 INFO [AnnotationBinder] Binding entity from annotated class: com.jumbo.model.system.SystemMessage
15:28:38,183 INFO [EntityBinder] Bind entity com.jumbo.model.system.SystemMessage on table T_SYS_MESSAGE
15:28:38,222 INFO [CollectionBinder] Mapping collection: com.jumbo.model.member.Member.memberAddresses -> T_MA_MEMBER_ADDRESS
15:28:38,225 INFO [CollectionBinder] Mapping collection: com.jumbo.model.member.Member.memberRanks -> T_MA_MEMBER_RANK
15:28:38,228 INFO [CollectionBinder] Mapping collection: com.jumbo.model.sales.ReturnApplication.appLines -> T_SO_RETURN_REQUEST_LINE
15:28:38,234 INFO [CollectionBinder] Mapping collection: com.jumbo.model.sales.ReturnOrder.roLines -> T_SO_RETURN_ORDER_LINE
15:28:38,236 INFO [CollectionBinder] Mapping collection: com.jumbo.model.sales.SalesOrder.soLines -> T_SO_SO_LINE
15:28:38,239 INFO [CollectionBinder] Mapping collection: com.jumbo.model.system.authorization.OperationUnit.childrenUnits -> T_AU_OPERATION_UNIT
15:28:38,244 INFO [CollectionBinder] Mapping collection: com.jumbo.model.system.authorization.OperationUnitType.ous -> T_AU_OPERATION_UNIT
15:28:38,254 INFO [AnnotationConfiguration] Hibernate Validator not found: ignoring
yes,没错
好了, hibernate.cfg.xml里面 就留下自己的参数配置
<session-factory>
<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" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" 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>
就可以把所有原来写在 hibernate.cfg.xml 里面的 mapping 给干掉了 ,以后没新增或者删除一个@entity,不用去动hibernate.cfg.xml
附加一句 , hibernate.cfg.xml 关于hibernate的参数也可以在 spring 里面配置,但是 个人认为这部分的参数,仍然在hibernate 里面 配置比较好,反正在spring 里面配置这些参数也得写这么多代码
最后,附上一个 我查询源码过程中的 一个 脑图 给大家参考下
参考资料:http://stackoverflow.com/questions/1413190/hibernate-mapping-package