用于 MATLAB 的 Java JPA 类

发布于 2024-10-06 13:54:53 字数 666 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夏见 2024-10-13 13:54:53

好吧,我找到了“答案”。之前我看到一篇关于 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.

夏末 2024-10-13 13:54:53

在 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 of classpath.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 place startup.m.

毁梦 2024-10-13 13:54:53

这只是您关于静态与动态类路径的答案的后续内容。这里有一个函数,可以让您诊断从 Matlab 中加载 Java 类的位置,以及是否存在类定义的任何屏蔽,这可能就是它对您的排序敏感的原因。您可能会看到其他碰撞;至少 dom4j.jar 和 commons-collections.jar 随 Matlab 一起提供,但我不知道是什么版本。

function whereisjavaclassloadingfrom(ClassName)
%WHEREISJAVACLASSLOADINGFROM  Show where a Java class is loaded from
%
% whereisjavaclassloadingfrom(ClassName)
%
% Shows where a Java class is loaded from in this Matlab session's JVM.
% This is for diagnosing Java class load problems, such as classpath
% ordering issues, seeing if a class of a given name is included in an
% unexpected JAR file, etc.
%
% Displays output to console.
%
% Examples:
%
% whereisjavaclassloadingfrom('java.util.HashMap')
% whereisjavaclassloadingfrom('com.ldhenergy.etools.MxUtil')
% whereisjavaclassloadingfrom('com.google.common.collect.Maps')
% whereisjavaclassloadingfrom('org.apache.commons.math.complex.Complex')

% Use javaArray to get Class object without having to instantiate. This
% lets it work with objects that have private or non-zero-arg constructors,
% and avoids side effects of object construction.
% (Would use java.lang.Class.forName(), because that's a more direct way of
% doing this, but it doesn't work for stuff on the dynamic classpath.)
ja = javaArray(ClassName,1);
klass = ja.getClass().getComponentType();

klassLoader = klass.getClassLoader();
if isempty(klassLoader)
    % JVM used null to represent the "bootstrap" class loader
    % I think that's the same as the "system" class loader
    klassLoader = java.lang.ClassLoader.getSystemClassLoader();
end
klassLoaderStr = char(klassLoader.toString());

klassFilePath = [strrep(ClassName, '.', '/') '.class'];
try
    % This logic assumes that the classes exist as files in the class
    % loader. It's a valid assumption for mainstream class loaders,
    % including the one's I've seen with Matlab.
    klassUrl = klassLoader.getResource(klassFilePath);
    if isempty(klassUrl)
        klassUrlStr = '';
    else
        klassUrlStr = char(klassUrl.toString());
    end
catch err
    klassUrlStr = sprintf('ERROR: %s', err.message);
end

% Get all locations, to reveal masked definitions
urls = enumeration2array(klassLoader.getResources(klassFilePath));

disp(sprintf('Version: %s\nClass:       %s\nClassLoader: %s\nURL:         %s', version,...
    char(klass.getName()), klassLoaderStr, klassUrlStr));
if numel(urls) > 1
    disp('Class is masked:');
    for i = 1:numel(urls)
        disp(sprintf('URL %d:       %s', i, char(urls(i))));
    end
end

%%
function out = enumeration2array(jenum)
tmp = {};
while jenum.hasMoreElements()
    tmp{end+1} = jenum.nextElement();
end
out = [tmp{:}];

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.

function whereisjavaclassloadingfrom(ClassName)
%WHEREISJAVACLASSLOADINGFROM  Show where a Java class is loaded from
%
% whereisjavaclassloadingfrom(ClassName)
%
% Shows where a Java class is loaded from in this Matlab session's JVM.
% This is for diagnosing Java class load problems, such as classpath
% ordering issues, seeing if a class of a given name is included in an
% unexpected JAR file, etc.
%
% Displays output to console.
%
% Examples:
%
% whereisjavaclassloadingfrom('java.util.HashMap')
% whereisjavaclassloadingfrom('com.ldhenergy.etools.MxUtil')
% whereisjavaclassloadingfrom('com.google.common.collect.Maps')
% whereisjavaclassloadingfrom('org.apache.commons.math.complex.Complex')

% Use javaArray to get Class object without having to instantiate. This
% lets it work with objects that have private or non-zero-arg constructors,
% and avoids side effects of object construction.
% (Would use java.lang.Class.forName(), because that's a more direct way of
% doing this, but it doesn't work for stuff on the dynamic classpath.)
ja = javaArray(ClassName,1);
klass = ja.getClass().getComponentType();

klassLoader = klass.getClassLoader();
if isempty(klassLoader)
    % JVM used null to represent the "bootstrap" class loader
    % I think that's the same as the "system" class loader
    klassLoader = java.lang.ClassLoader.getSystemClassLoader();
end
klassLoaderStr = char(klassLoader.toString());

klassFilePath = [strrep(ClassName, '.', '/') '.class'];
try
    % This logic assumes that the classes exist as files in the class
    % loader. It's a valid assumption for mainstream class loaders,
    % including the one's I've seen with Matlab.
    klassUrl = klassLoader.getResource(klassFilePath);
    if isempty(klassUrl)
        klassUrlStr = '';
    else
        klassUrlStr = char(klassUrl.toString());
    end
catch err
    klassUrlStr = sprintf('ERROR: %s', err.message);
end

% Get all locations, to reveal masked definitions
urls = enumeration2array(klassLoader.getResources(klassFilePath));

disp(sprintf('Version: %s\nClass:       %s\nClassLoader: %s\nURL:         %s', version,...
    char(klass.getName()), klassLoaderStr, klassUrlStr));
if numel(urls) > 1
    disp('Class is masked:');
    for i = 1:numel(urls)
        disp(sprintf('URL %d:       %s', i, char(urls(i))));
    end
end

%%
function out = enumeration2array(jenum)
tmp = {};
while jenum.hasMoreElements()
    tmp{end+1} = jenum.nextElement();
end
out = [tmp{:}];
总以为 2024-10-13 13:54:53

确保您的类路径上有 JPA 提供程序 jar(例如 eclipselink.jar)。

Ensure that you have a JPA provider jar (such as eclipselink.jar) on your classpath.

那片花海 2024-10-13 13:54:53

您是否绝对确定您在调用中正确拼写了持久性单元的名称:

javax.persistence.Persistence.createEntityManagerFactory(String puName)

这也会给您带来相同的错误。该名称区分大小写。

Are you absolutely certain that you have spelled the name of the persistence unit correctly in the call to:

javax.persistence.Persistence.createEntityManagerFactory(String puName)

That would also give you the same error. The name is case-sensitive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文