用于 MATLAB 的 Java JPA 类
我在 Windows XP Pro SP3 上使用 MATLAB R2007b、Java 1.6 SE、Eclipse Helios 和 MySql 5。
我正在尝试创建一个使用 JPA 注释访问 MySql 5 数据库的类库。这个想法是,MATLAB 脚本实例化这些 Java 对象,这些对象提供用于访问 DB 的 API。
我可以创建我的带注释的类,它在 Eclipse 中工作(即 JUnit 测试)。我可以将代码导出到 jar,然后可以从命令提示符运行它。
我使用 javaaddpath() 更新 MATLAB Java 类路径。我可以在 MATLAB 中实例化我的类。但是,当我调用 init()(它调用 javax.persistence.Persistence.createEntityManagerFactory())时,我收到可怕的
“没有 EntityManager 的持久性提供程序”
此错误通常意味着 persistence.xml 文件不在正确的位置。但这一定是因为我的 jar 从命令行运行。将 META-INF 文件夹添加到 MATLAB java 类路径没有帮助。无论是否添加 META-INF,也不会提取 jar 并将提取的文件夹结构添加到类路径中。
有没有人有任何想法,无论是否疯狂?有没有人在任何版本的 MATLAB 中这样做过?
谢谢。
-赖利。
I'm using MATLAB R2007b, Java 1.6 SE, Eclipse Helios, and MySql 5 on Windows XP Pro SP3.
I'm trying to create a class library that uses JPA annotations to access a MySql 5 database. The idea is that the MATLAB script instantiates these Java objects, which provide an API for accessing the DB.
I can create my annotated classes, which work within Eclipse (i.e. JUnit test). I can export the code to a jar, which I can run from a command prompt.
I update the MATLAB Java classpath using javaaddpath(). I can instantiate my class within MATLAB. But when I call my init(), which calls javax.persistence.Persistence.createEntityManagerFactory(), I get the dreaded
"No Persistence provider for EntityManager"
This error usually means that the persistence.xml file isn't in the right place. But it must be because my jar works from the command line. Adding the META-INF folder to the MATLAB java classpath doesn't help. Nor does extracting the jar and adding the extracted folder structure to the classpath, whether or not META-INF is added.
Does anyone have any ideas, nutty or not? Has anyone ever done this in any version of MATLAB.
Thanks.
-reilly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,我找到了“答案”。之前我看到一篇关于 MATLAB 的“动态”和“静态”cp 差异的帖子。 “静态”cp 是在启动时加载的文本文件。 “动态”cp 在运行时加载,您通常通过 m 脚本调用来操作它。这就是我想要做的。
所以我将罐子添加到动态路径中,但它不起作用。
我将它们添加到静态路径的末尾并得到了不同的错误,这似乎与 XML 解析有关。进步!
然后我将我的罐子添加到静态路径的开头,它就可以工作了。
引用巴特·辛普森的话:Craptackular。
感谢您的所有想法。问我一个 C# 问题,这样我就可以回复...-
reilly。
Well, I found "an answer." Somewhere before I saw a post about the difference in MATLAB's "dynamic" and "static" cp's. The "static" cp is a text file that is loaded at startup. The "dynamic" cp is loaded at runtime and you typically manipulate it with m-script calls. That's what I was trying to do.
So I added my jars to the dynamic path and it didn't work.
I added them to the end of the static path and got DIFFERENT errors, which seemed to pertain to the XML parsing. Progress!
Then I added my jars to the BEGINNING of the static path and it works.
To quote Bart Simpson: Craptackular.
Thanks for all your ideas. Ask me a C# question so I can reciprocate...
-reilly.
在 MATLAB 中使用 Java 时,我经常遇到动态类路径问题。作为解决方法,使用
classpath.txt
已经解决了迄今为止的所有问题。处理不同的环境(例如测试和生产)会在 MATLAB 启动目录中产生多个
classpath.txt
文件。使用不同的 MATLAB 版本会进一步增加classpath.txt
文件的数量。ClassPathHacker.java 是动态添加的选项 类和 jar 文件到您的静态 类路径。使用这种方法,无需再接触
classpath.txt
。您的 Java 类路径配置可以保留在预期位置startup.m
中。While working with Java in MATLAB, I often encountered issues with the dynamic classpath. As a workaround, using
classpath.txt
has solved any issue so far.Dealing with different environments, for example testing and production, results in multiple
classpath.txt
files in your MATLAB start directory. Using different MATLAB releases adds another multiplier to the number ofclasspath.txt
files around.ClassPathHacker.java is an option to add dynamically classes and jar files to your static classpath. Using this approach there is no need to touch
classpath.txt
anymore. Your Java classpath configuration can stay in the intended placestartup.m
.这只是您关于静态与动态类路径的答案的后续内容。这里有一个函数,可以让您诊断从 Matlab 中加载 Java 类的位置,以及是否存在类定义的任何屏蔽,这可能就是它对您的排序敏感的原因。您可能会看到其他碰撞;至少 dom4j.jar 和 commons-collections.jar 随 Matlab 一起提供,但我不知道是什么版本。
This is just a followup to your answer about static vs dynamic classpaths. Here's a function which will let you diagnose where a Java class is being loaded from within Matlab, and if there's any masking of the class definitions, which could be why it was sensitive to ordering for you. You might see other collisions; at least dom4j.jar and commons-collections.jar are shipped with Matlab, but I don't know what versions.
确保您的类路径上有 JPA 提供程序 jar(例如 eclipselink.jar)。
Ensure that you have a JPA provider jar (such as eclipselink.jar) on your classpath.
您是否绝对确定您在调用中正确拼写了持久性单元的名称:
这也会给您带来相同的错误。该名称区分大小写。
Are you absolutely certain that you have spelled the name of the persistence unit correctly in the call to:
That would also give you the same error. The name is case-sensitive.