在使用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
最满意答案
我不确定你是否看到他们,但仔细看看这些引号:
[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
return
s 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 thefor
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"]]
相关问答
更多-
如果内存消耗和时间不是问题,最简单的方法是从文件读取所有数据,更改数据并用新数据覆盖文件: 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): if re. ...
-
通过匹配行中的数字,使用python删除.txt中的一行(delete a line in .txt using python by matching the number in the line)[2022-11-17]
我不确定你是否看到他们,但仔细看看这些引号: [1,'1','3/03/2011', [“ScrewDriver”, “Hammer”, “Stone”]] 这是有区别的: ” 和这个: " 第一个( ” )是一个引用解释器的引用。 使用普通引号( " )并回传你的脚本返回的内容。 另外,这个return语句做了什么: return (new) 如果你在函数中,函数会在列表的第一次迭代时退出,因为它会在每一个循环中return ,但是只有第一个被捕获并且函数在那里死掉。 我将return语句向下移动 ... -
匹配txt文件中的每个单词(Matching every word in a txt file)[2021-07-27]
如果文件的格式与你说的一样,即 这是一条线 格式如下:“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 say ... -
如何将.txt内容导入python 2.7中的一行代码(how to import .txt contents into a line of code in python 2.7)[2022-04-01]
这假设您的文件是表单 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数组中。 上述解决方案可行, ... -
基本上你需要将它复制到另一个文件 - 你不能在“就地”文件中删除(或插入)数据。 例如,在.NET 4中很容易: var lines = File.ReadLines("input.txt").Skip(1); File.WriteAllLines("output.txt", lines); 在旧版本的.NET中稍微多做一些工作,至少如果你想避免将整个文件加载到内存中,但仍然不太难。 如果您需要旧版本的代码(并说明哪个版本),请告诉我。 编辑:正如评论中所讨论的, 如果您愿意将整个文件加载到内存中,您可以 ...
-
如果我理解你的话: 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 understood y ...
-
您可以使用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] I ...
-
目前,您只是阅读每一行而不是写入文件。 以写入模式重新打开文件并将完整的字符串写入其中,如下所示: 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 only ...
-
替换python代码中的字符串并减少下一行中的数字(replace strings in python code and decrement number in the next line)[2022-03-15]
我觉得我有点沮丧,最后为你编写代码...... 请在下次发布之前阅读如何创建最小,完整且可验证的示例 ,因为这被认为是不礼貌的... 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.txt' ... -
如何逐行读取python中的txt文件并将每一行设置为变量(How to read txt file in python line by line and set each line to a variable)[2022-02-28]
如果使用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 string. ...