如何获取 Eclipselink 实体管理器?

发布于 2024-12-17 06:25:41 字数 1434 浏览 2 评论 0原文

我正在开发一个 C/S java 项目,并尝试使用 Eclipselink 来做 ORM(客户端缓存数据库,derby)。 通过示例和示例进行查询非常容易。表达式,但是当涉及到插入/更新时,我发现应该使用实体管理器(我对java ORM很陌生,也许还有另一种方法?)。 当我尝试创建它们时,它总是抛出一个异常,告诉我我需要一个服务器会话(即无法按名称找到持久性单元)。 当我使用会话bean & @PersistenceUnit注解,它返回一个Null。

我犯了一些愚蠢的错误吗?或者有没有简单的方法来更新/插入值?我认为可能有某种类似 session.save(Object) 或类似的方法可以使这项工作变得简单。

编辑:我尝试使用 UnitOfWork,但收到“尝试修改身份列”错误,我应该更改映射文件或使 Eclipselink 识别身份 PK 的文件吗?

@GenerateValue(strategy = GenerationType.IDENTITY)

在 POJO 中添加此行不起作用。

编辑:我尝试使用工作单元插入值,这是代码

    SessionFactory sessionFactory = new SessionFactory("default");
    Session session = sessionFactory.getSharedSession();
    UnitOfWork uow = session.acquireUnitOfWork();
    SysUser usr2 = new SysUser();
    usr2.setUserName("test");
    usr2.setUserPassword("test");
    uow.registerObject(usr2);
    uow.commit();

,但它导致

Internal Exception: java.sql.SQLSyntaxErrorException: Attempt to modify an identity column 'DB_ID'.

这里是我使用的注释:

@Column(name = "DB_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
protected int dbId;

对于 EntityManager:

<persistence-unit name="my-app-name">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:/app/jdbc/jdbc/ConnectionDerbyDS</jta-data-source>

I'm working on a C/S java project and try to use Eclipselink to do the ORM(client side cache database,derby).
It's quite easy to do the query with examples & expressions, but when it comes to insert/update, i found that Entity Manager should be used(I'm quite new in java ORM, maybe there's another way?).
When i try to create em, it always throw a Exception that telling me i need a server session(i.e. can not find the persistence-unit by name).
When i use session bean & @PersistenceUnit annotation, it returns a Null.

Did I make some stupid mistakes? Or does there any easy way to Update/Insert values? I supposed that there may be some way like session.save(Object) or something like that which can makes this work simple.

EDIT: I'v tried to use UnitOfWork, but get "Attempt to modify an identity column" error, should i change map file or something that make Eclipselink recognize the identity PK?

@GeneratedValue(strategy = GenerationType.IDENTITY)

Add this line in POJO do not work.

EDIT:I've tried use Unit of work to insert value here's the code

    SessionFactory sessionFactory = new SessionFactory("default");
    Session session = sessionFactory.getSharedSession();
    UnitOfWork uow = session.acquireUnitOfWork();
    SysUser usr2 = new SysUser();
    usr2.setUserName("test");
    usr2.setUserPassword("test");
    uow.registerObject(usr2);
    uow.commit();

But it result in a

Internal Exception: java.sql.SQLSyntaxErrorException: Attempt to modify an identity column 'DB_ID'.

here's the annotation i used:

@Column(name = "DB_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
protected int dbId;

And for EntityManager:

<persistence-unit name="my-app-name">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:/app/jdbc/jdbc/ConnectionDerbyDS</jta-data-source>

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

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

发布评论

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

评论(1

爱本泡沫多脆弱 2024-12-24 06:25:41

当您在 Java SE 应用程序中使用 EclipseLink(通过 JPA API)时,您的设置必须与您的应用程序由在 Java EE 容器中运行的组件组成时略有不同。

您将获得一个包含如下行的 EntityManager:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("my-app");
EntityManager em = emf.createEntityManager();

并且您的应用程序需要在 JAR 的 META-INF 目录中拥有一个 persistence.xml 文件您的申请文件。您在此文件中配置JPA;您在那里放置的内容告诉 Java 要使用哪个持久性提供程序以及数据库的连接参数是什么。

持久化单元名称(上例中的"my-app")必须与 persistence.xml 中的 persistence-unit 元素中指定的名称相同

请参阅此 EclipseLink 示例,它显示了您的 persistence.xml必须看起来像。

EntityManager 包含诸如 persist(Object) 之类的方法,您可以使用它们将实体保存到数据库中(请参阅这些示例展示了如何使用 EntityManager)。

您的实体 POJO 应该有一列包含实体 id,并且它必须有一个无参数构造函数:

@Entity
public class Example {

    @Id
    @GeneratedValue
    private long id;

    // ... Add other properties here

    // Required no-args constructor
    public Example() {
    }

    // ... Add getters and setters here
}

Addition

您可以使用 EntityManager 在数据库中保存实体就像下面的代码一样。无需使用 UnitOfWork (据我所知,UnitOfWork 是使用较旧的 JPA 1.0 API 时需要的东西;对于 JPA 2.0,则不需要它):

entityManager.getTransaction().begin();
entityManager.persist(myEntity);
entityManager.getTransaction().commit();

When you use EclipseLink (via the JPA API) in a Java SE application, you have to set up things a little differently then when your app consists of components running in a Java EE container.

You get an EntityManager with lines like these:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("my-app");
EntityManager em = emf.createEntityManager();

And your application needs to have a persistence.xml file in the META-INF directory of the JAR file of your application. You configure JPA in this file; things you put in there tell Java which persistence provider to use and what the connection parameters for your database are.

The persistence unit name ("my-app" in the example above) must be the same as the name specified in the persistence-unit element in persistence.xml.

See this EclipseLink example, which shows what your persistence.xml has to look like.

EntityManager contains methods such as persist(Object) which you can use to save entities into the database (see these examples that show how to use EntityManager).

Your entity POJO should have a column which will contain the entity id, and it must have a no-args constructor:

@Entity
public class Example {

    @Id
    @GeneratedValue
    private long id;

    // ... Add other properties here

    // Required no-args constructor
    public Example() {
    }

    // ... Add getters and setters here
}

Addition

You can save an entity in the database using the EntityManager like in the following code. No need to use UnitOfWork (as far as I know, UnitOfWork is something you needed when using the older JPA 1.0 API; you don't need this for JPA 2.0):

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