如何获取 Eclipselink 实体管理器?
我正在开发一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在 Java SE 应用程序中使用 EclipseLink(通过 JPA API)时,您的设置必须与您的应用程序由在 Java EE 容器中运行的组件组成时略有不同。
您将获得一个包含如下行的 EntityManager:
并且您的应用程序需要在 JAR 的 META-INF 目录中拥有一个 persistence.xml 文件您的申请文件。您在此文件中配置JPA;您在那里放置的内容告诉 Java 要使用哪个持久性提供程序以及数据库的连接参数是什么。
持久化单元名称(上例中的
"my-app"
)必须与persistence.xml 中的
。persistence-unit
元素中指定的名称相同请参阅此 EclipseLink 示例,它显示了您的
persistence.xml必须看起来像。
EntityManager
包含诸如persist(Object)
之类的方法,您可以使用它们将实体保存到数据库中(请参阅这些示例展示了如何使用EntityManager
)。您的实体 POJO 应该有一列包含实体 id,并且它必须有一个无参数构造函数:
Addition
您可以使用
EntityManager
在数据库中保存实体就像下面的代码一样。无需使用UnitOfWork
(据我所知,UnitOfWork
是使用较旧的 JPA 1.0 API 时需要的东西;对于 JPA 2.0,则不需要它):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:And your application needs to have a
persistence.xml
file in theMETA-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 thepersistence-unit
element inpersistence.xml
.See this EclipseLink example, which shows what your
persistence.xml
has to look like.EntityManager
contains methods such aspersist(Object)
which you can use to save entities into the database (see these examples that show how to useEntityManager
).Your entity POJO should have a column which will contain the entity id, and it must have a no-args constructor:
Addition
You can save an entity in the database using the
EntityManager
like in the following code. No need to useUnitOfWork
(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):