使用 EclipseLink,在每个新连接上向数据库发送一条语句

发布于 2024-11-11 17:28:21 字数 525 浏览 3 评论 0原文

我正在使用 EclipseLink 访问 SQLite 数据库。默认情况下,由于向后兼容性,SQLite 不强制执行外键约束。可以使用 connection.createStatement().execute("PRAGMAforeign_keys=ON") 在每个连接上启用外键约束。

使用 JDBC 时,以下代码可以解决问题:

Connection connection = DriverManager.getConnection("jdbc:sqlite:example.db");
Statement statement = connection.createStatement();
statement.execute("PRAGMA foreign_keys=ON");
// From now on, foreign key constraints are enforced on 'connection'

How will I get the sameeffect with JPA/EclipseLink?

I'm using EclipseLink to access an SQLite database. SQLite, by default, because of backwards compatibility, does not enforce foreign key constraints. Foreign keys constraints can be enabled on per-connection basis using connection.createStatement().execute("PRAGMA foreign_keys=ON").

When using JDBC, the following code does the trick:

Connection connection = DriverManager.getConnection("jdbc:sqlite:example.db");
Statement statement = connection.createStatement();
statement.execute("PRAGMA foreign_keys=ON");
// From now on, foreign key constraints are enforced on 'connection'

How would I get the same effect with JPA/EclipseLink?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

海拔太高太耀眼 2024-11-18 17:28:21

在 persistence.xml 中 >持久性单位>属性,添加:

<property name="eclipselink.session-event-listener"
value="com.example.MySessionEventAdapter"/>

创建一个类MySessionEventAdapter:

package com.example;

import org.eclipse.persistence.sessions.SessionEvent;
import org.eclipse.persistence.sessions.SessionEventAdapter;

public class MySessionEventAdapter extends SessionEventAdapter {

    @Override
    public void postAcquireClientSession(SessionEvent event) {

        event.getSession().executeNonSelectingSQL("PRAGMA foreign_keys=ON");

        super.postAcquireClientSession(event);

    }
}

in persistence.xml > persitence-unit > properties, add:

<property name="eclipselink.session-event-listener"
value="com.example.MySessionEventAdapter"/>

Create a class MySessionEventAdapter:

package com.example;

import org.eclipse.persistence.sessions.SessionEvent;
import org.eclipse.persistence.sessions.SessionEventAdapter;

public class MySessionEventAdapter extends SessionEventAdapter {

    @Override
    public void postAcquireClientSession(SessionEvent event) {

        event.getSession().executeNonSelectingSQL("PRAGMA foreign_keys=ON");

        super.postAcquireClientSession(event);

    }
}
涫野音 2024-11-18 17:28:21

您可以使用本机查询。

em.createNativeQuery("PRAGMAforeign_keys=ON").executeUpdate();

您还可以在 persistence.xml 中注册一个 EclipseLink SessionEventListener,以便始终对每个连接(postAcquireConnection 事件)执行此操作。

您还可以在数据库上配置它,以避免在每个连接上执行此 SQL。

You can use a native query.

em.createNativeQuery("PRAGMA foreign_keys=ON").executeUpdate();

You could also register an EclipseLink SessionEventListener in your persistence.xml to have this always done for every connection (postAcquireConnection event).

You may also be able to configure it on your database, to avoid having to execute this SQL on every connection.

自此以后,行同陌路 2024-11-18 17:28:21

Xerial JDBC-Driver for SQLite 支持将“foreign_keys”PRAGMA 设置为 DriverManager.getConnection() 中的属性:

Properties props = new Properties();
props.put("foreign_keys", "true");
DriverManager.getConnection("jdbc:sqlite:example.db", props);
...

另请参阅 java.org.sqlite.SQLiteConfig

有没有办法使用 EclipseLink 将此类 JDBC 驱动程序特定的连接属性添加到 persistance.xml 中?
这种简单的方法,只是将其添加到属性部分,对我来说不起作用。

The Xerial JDBC-Driver for SQLite supports to set the "foreign_keys" PRAGMA as a property in DriverManager.getConnection():

Properties props = new Properties();
props.put("foreign_keys", "true");
DriverManager.getConnection("jdbc:sqlite:example.db", props);
...

See also java.org.sqlite.SQLiteConfig.

Is there a way to add such JDBC-Driver specific connection properties to persistance.xml with EclipseLink?
The trivial approach, just adding it to the properties-section, did not work for me.

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