首页 \ 问答 \ 在使用Spring 3 + Hibernate JPA时发现JPA注释类(Discover JPA annotated classes when using Spring 3+Hibernate JPA)

在使用Spring 3 + Hibernate JPA时发现JPA注释类(Discover JPA annotated classes when using Spring 3+Hibernate JPA)

我有一个使用Spring 3 + Hibernate JPA堆栈的Web应用程序。

我想知道是否有办法让Hibernate自动发现@Entity带注释的类,这样我就不必在persistence.xml文件中列出它们。 我的@Entity注释类“活着”在位于我的Web应用程序的WEB-INF / lib中的单独的jar中。

这是我的Spring配置文件中的一个片段:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
   <property name="persistenceUnitName" value="mypersistence"/>
   <property name="dataSource" ref="dataSource"/>
   <property name="jpaVendorAdapter">
       <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
           <property name="showSql" value="true"/>
           <property name="generateDdl" value="true"/>
           <property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect"/>
       </bean>
   </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
    <property name="url" value="jdbc:derby://localhost:1527/library;create=true"/>
    <property name="username" value="app"/>
    <property name="password" value="app"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<bean id="persistenceAnnotation" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

I have a web application using the Spring 3 + Hibernate JPA stack.

I would like to know if there is a way to have Hibernate to automatically discover @Entity annotated classes, so that I don't have to list them in the persistence.xml file. My @Entity annotated classes "live" in a separate jar, located in the WEB-INF/lib of my web application.

This is a snippet from my Spring configuration file:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
   <property name="persistenceUnitName" value="mypersistence"/>
   <property name="dataSource" ref="dataSource"/>
   <property name="jpaVendorAdapter">
       <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
           <property name="showSql" value="true"/>
           <property name="generateDdl" value="true"/>
           <property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect"/>
       </bean>
   </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
    <property name="url" value="jdbc:derby://localhost:1527/library;create=true"/>
    <property name="username" value="app"/>
    <property name="password" value="app"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<bean id="persistenceAnnotation" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

原文:https://stackoverflow.com/questions/4872705
更新时间:2023-07-08 17:07

最满意答案

我不确定你是否看到他们,但仔细看看这些引号:

[1,'1','3/03/2011', [“ScrewDriver”, “Hammer”, “Stone”]]

这是有区别的:

和这个:

"

第一个( )是一个引用解释器的引用。 使用普通引号( " )并回传你的脚本返回的内容。

另外,这个return语句做了什么:

return (new)

如果你在函数中,函数会在列表的第一次迭代时退出,因为它会在每一个循环中return ,但是只有第一个被捕获并且函数在那里死掉。

我将return语句向下移动一级,所以它不是for循环的一部分。 所以改变这个:

for line in lines:
    listline=eval(line)
    if not(num_del==list_line[0]):
        new +=(line + "\n")
    return (new)

为此:

for line in lines:
    listline=eval(line)
    if not(num_del==list_line[0]):
        new +=(line + "\n")

return (new)

这是没有引号的文本文件。 尝试复制并粘贴它:

[99 ,1, "3/03/2011", ["ScrewDriver", "Hammer", "Stone"]]
[2 ,2, "3/03/2011", ["hammer,nails"]]
[31 ,2, "3/03/2011", ["plaster,studd"]]
[100 ,2, "3/03/2011", ["hammer,studd"]]

I'm not sure if you see them, but look closely at those quotation marks:

[1,'1','3/03/2011', [“ScrewDriver”, “Hammer”, “Stone”]]

There's a difference between this:

and this:

"

The first one () is a curly quote, which screws up the interpreter. Use normal quotes (") and post back what your script returns then.

Also, what does this return statement do:

return (new)

If you're in a function, the function will quit upon the first iteration of the list, as it returns every single loop, but only the first one is caught and the function dies there.

I'd move the return statement down a level so it isn't part of the for loop. So change this:

for line in lines:
    listline=eval(line)
    if not(num_del==list_line[0]):
        new +=(line + "\n")
    return (new)

To this:

for line in lines:
    listline=eval(line)
    if not(num_del==list_line[0]):
        new +=(line + "\n")

return (new)

This is your text file without the curly quotes. Try copy and pasting it:

[99 ,1, "3/03/2011", ["ScrewDriver", "Hammer", "Stone"]]
[2 ,2, "3/03/2011", ["hammer,nails"]]
[31 ,2, "3/03/2011", ["plaster,studd"]]
[100 ,2, "3/03/2011", ["hammer,studd"]]

相关问答

更多

相关文章

更多

最新问答

更多
  • 如何检索Ember.js模型的所有属性(How to retrieve all properties of an Ember.js model)
  • maven中snapshot快照库和release发布库的区别和作用
  • arraylist中的搜索元素(Search element in arraylist)
  • 从mysli_fetch_array中获取选定的值并输出(Get selected value from mysli_fetch_array and output)
  • Windows Phone上的可用共享扩展(Available Share Extensions on Windows Phone)
  • 如何在命令提示符下将日期设置为文件名(How to set file name as date in command prompt)
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • 从iframe访问父页面的id元素(accessing id element of parent page from iframe)
  • linux的常用命令干什么用的
  • Feign Client + Eureka POST请求正文(Feign Client + Eureka POST request body)
  • 怎么删除禁用RHEL/CentOS 7上不需要的服务
  • 为什么Gradle运行测试两次?(Why does Gradle run tests twice?)
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在android中的活动之间切换?(Switching between activities in android?)
  • Perforce:如何从Depot到Workspace丢失文件?(Perforce: how to get missing file from Depot to Workspace?)
  • Webform页面避免运行服务器(Webform page avoiding runat server)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 内存布局破解(memory layout hack)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • 我们可以有一个调度程序,你可以异步添加东西,但会同步按顺序执行吗?(Can we have a dispatcher that you can add things todo asynchronously but will be executed in that order synchronously?)
  • “FROM a,b”和“FROM a FULL OUTER JOIN b”之间有什么区别?(What is the difference between “FROM a, b” and “FROM a FULL OUTER JOIN b”?)
  • Java中的不可变类(Immutable class in Java)
  • bat批处理文件结果导出到txt
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • 德州新起点计算机培训学校主要课程有什么?
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • “latin1_german1_ci”整理来自哪里?(Where is “latin1_german1_ci” collation coming from?)