在使用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
2024-01-17 19:01

满意答案

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

[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"]]

相关问答

更多

在Python 3.3中写入txt文件的上一行(Writing to the previous line of a txt file in Python 3.3)

如果内存消耗和时间不是问题,最简单的方法是从文件读取所有数据,更改数据并用新数据覆盖文件: import re new_num = 456 new_data = str(new_num) + '\n' + 'Georph\n' written = False with open('line_search.txt','r') as f: data = f.readlines() updated_data = [] for i, line in enumerate(data): i...

通过匹配行中的数字,使用python删除.txt中的一行(delete a line in .txt using python by matching the number in the line)

我不确定你是否看到他们,但仔细看看这些引号: [1,'1','3/03/2011', [“ScrewDriver”, “Hammer”, “Stone”]] 这是有区别的: ” 和这个: " 第一个( ” )是一个引用解释器的引用。 使用普通引号( " )并回传你的脚本返回的内容。 另外,这个return语句做了什么: return (new) 如果你在函数中,函数会在列表的第一次迭代时退出,因为它会在每一个循环中return ,但是只有第一个被捕获并且函数在那里死掉。 我将return语...

匹配txt文件中的每个单词(Matching every word in a txt file)

如果文件的格式与你说的一样,即 这是一条线 格式如下:“MARIA”,“SUSAN”,“ANGELA”,“JACK” 那应该工作: >>> import csv >>> lines = csv.reader(open('words.txt', 'r'), delimiter=',') >>> words = lines.next() >>> words ['MARIA', 'SUSAN', 'ANGELA', 'JACK'] If the format of the file is as you...

如何将.txt内容导入python 2.7中的一行代码(how to import .txt contents into a line of code in python 2.7)

这假设您的文件是表单 AAPL MSFT IBM 等等。 您的文件名为tickers.txt 。 在Pandas做到这一点 import pandas as pd tickers = pd.read_csv('tickers.txt', names=['Tickers']) 这会将文件加载到数据框中。 在Numpy做的 import numpy as np tickers = np.loadtxt('ticker.txt', dtype=str) 这会将文件加载到numpy数组中。 上述解决...

如何删除txt文件中的第一行(how to delete first line in a txt file)

基本上你需要将它复制到另一个文件 - 你不能在“就地”文件中删除(或插入)数据。 例如,在.NET 4中很容易: var lines = File.ReadLines("input.txt").Skip(1); File.WriteAllLines("output.txt", lines); 在旧版本的.NET中稍微多做一些工作,至少如果你想避免将整个文件加载到内存中,但仍然不太难。 如果您需要旧版本的代码(并说明哪个版本),请告诉我。 编辑:正如评论中所讨论的, 如果您愿意将整个文件加载到内存...

如何在Python中访问文件(.txt)中的下一行(How to access next line in a file(.txt) in Python)

如果我理解你的话: b = open(a, 'r+') for line in b: if line.startswith("E PRAM") and "OOPS: 1" in line: next_line = next(b) # do whatever you need 文件提供了所谓的“迭代器协议”,这就是为什么它们在-loops中工作的原因。 如果需要,您也可以手动调用它们的next功能。 查看PEP-234了解更多详情。 If I underst...

使用.txt文件行的内容作为python3变量的输入(Use content of .txt file line as input for python3 variable)

您可以使用readlines并获取列表的最后一项: file = open("primes.txt", "r").readlines() x = file[len(file)-1] 我认为这应该工作。 你可以通过拆分或类似的东西来摆脱2“[”和“]”。 You can do use readlines and get the last item of the list: file = open("primes.txt", "r").readlines() x = file[len(file)-1...

使用python将字符附加到txt文件中的每一行(Appending characters to each line in a txt file with python)

目前,您只是阅读每一行而不是写入文件。 以写入模式重新打开文件并将完整的字符串写入其中,如下所示: newf="" with open('helloworld.txt','r') as f: for line in f: newf+=line.strip()+"p\n" f.close() with open('helloworld.txt','w') as f: f.write(newf) f.close() Currently, you are...

替换python代码中的字符串并减少下一行中的数字(replace strings in python code and decrement number in the next line)

我觉得我有点沮丧,最后为你编写代码...... 请在下次发布之前阅读如何创建最小,完整且可验证的示例 ,因为这被认为是不礼貌的... def magic(line): # returns the first number in that line return [int(s) for s in line.split() if s.isdigit()][0] p0 = ["x", "y", "z"] # open the input file with open('replace...

如何逐行读取python中的txt文件并将每一行设置为变量(How to read txt file in python line by line and set each line to a variable)

如果使用rstrip()它将删除所有空格以及新行( \n )。 因此,请使用rstrip('\n')仅删除换行符。 当你想循环它时,将逻辑放在for循环中。 f = open('words.txt') for line in f.readlines(): secret = line.rstrip('\n') if (secret[-1:] == "\n"): print "Error, new line character at the end of the st...

相关文章

更多

jpa与hibernate注解混合使用

请问jpa的注解与hibernate的注解能混合使用吗?二者是什么关系?我的意思是我用的是jpa,但是 ...

spring 3.0 和jpa 整合 用jboss

1、网上说了很多例子,这就不说了,只介绍下我遇见的问题 java.lang.ClassCastExce ...

Spring +JPA 多持久化单元 怎么配置

Spring +JPA 多持久化单元 怎么配置

【第八章】 对ORM的支持 之 8.4 集成JPA ——跟我学spring3

JPA全称为Java持久性API(Java Persistence API),JPA是Java EE ...

JPA环境配置

本章将指导完成JPA在Windows和Linux系统的设置过程

Spring MVC 3 深入总结

一、前言: 大家好,Spring3 MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来 ...

基于struts2.18+spring3.0+hibernate3.5+annotation的用户权限

本项目地址:http://xiayingjie.iteye.com/blog/856922 1.系统 ...

Spring与Hibernate集成中的session问题

主要讨论Spring与Hibernate集成中的session问题 1.通过getSession()方 ...

Spring Data: a new perspective of data operations

Spring Data: a new perspective of data operations ...

JPA ORM框架介绍

标签定义的属性(在表中的字段)

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本