为什么 JPA 不自动为我的行生成 id?

发布于 2024-12-23 01:54:36 字数 1793 浏览 2 评论 0原文

我想使用 SEQUENCE 策略来自动生成 ids,但我为了让它工作而绞尽脑汁。我完全不知道为什么我不能实现它。

这就是我所做的。 首先我有一个实体:

 @Entity
@SequenceGenerator(name="VlasnikSeq", sequenceName="VLA_SEQ")
public class Vlasnik implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="VlasnikSeq")
    private Long id;
    //...

在 persistence.xml 中我将其映射:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="sampleAplication">
    <class>entities.Vlasnik</class>
    <class>entities.Ljubimac</class>
    </persistence-unit>
</persistence>

当我使用 Eclipses 功能为实体生成表时,我得到:

在此处输入图像描述

正如您所看到的,表已创建,但没有 SEQUENCE 表。 当 JPA 创建表时,我还在控制台中注意到以下消息:

[EL 警告]:异常 [EclipseLink-4002](Eclipse 持久性 服务 - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException 内部 异常:java.sql.SQLSyntaxErrorException:SEQUENCE 'VLA_SEQ' 确实 不存在。

我接下来要做的就是尝试运行该应用程序,看看是否可以创建一些行。但是当我尝试坚持某事时,我得到一个例外:

org.apache.derby.client.am.SqlException:SEQUENCE 'VLA_SEQ' 没有

所以我得出的结论是,由于某种原因我需要该表,所以我进入数据库管理视角并尝试执行以下查询:

CREATE SEQUENCE VLA_SEQ;

但我收到以下消息:

序列“VLA_SEQ”已存在。

我完全困惑了。我不知道我该怎么办。我只想在数据库中创建新行时自动生成实体的 ID。

这是我第一次使用 glassfish 3.1,在 3.0 版本中我不记得了,有这个问题,我什至可以只使用@GenneeratedValue。 我将不胜感激一些帮助。

I want to use SEQUENCE strategy for automatically generate ids, but i am breaking my head to make it work. I dont know at all why i cant make it happen.

This is what i do.
First i have an entity:

 @Entity
@SequenceGenerator(name="VlasnikSeq", sequenceName="VLA_SEQ")
public class Vlasnik implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="VlasnikSeq")
    private Long id;
    //...

in the persistence.xml i have it mapped:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="sampleAplication">
    <class>entities.Vlasnik</class>
    <class>entities.Ljubimac</class>
    </persistence-unit>
</persistence>

When i use eclipses feature for generating tables for entities i get this:

enter image description here

As you can see the tables are created, but there is no SEQUENCE table.
I also noticed in the console, the following message, while JPA was creating the tables:

[EL Warning]: Exception [EclipseLink-4002] (Eclipse Persistence
Services - 2.3.0.v20110604-r9504):
org.eclipse.persistence.exceptions.DatabaseException Internal
Exception: java.sql.SQLSyntaxErrorException: SEQUENCE 'VLA_SEQ' does
not exist.

The next think i do is try to run the app and see if i can create some rows. But when i try to persist something, i get an exception that says:

org.apache.derby.client.am.SqlException: SEQUENCE 'VLA_SEQ' does not

So i come to the conclusion that for some reason i need that table, so i go to the database management perspective and i try to execute the following query:

CREATE SEQUENCE VLA_SEQ;

But i get the following message:

Sequence 'VLA_SEQ' already exists.

I am totally confused. I don't know what should i do. I just want to automatically generate the IDs of my entities when a new row is created in the DB.

It is the first time i use glassfish 3.1, in the version 3.0 i dont remember, having this issue, i could even use just @GenneratedValue.
Ill appreciate some help.

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

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

发布评论

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

评论(1

肤浅与狂妄 2024-12-30 01:54:36

为了使用您必须指定的策略和生成器。默认情况下,策略为自动,生成器为

可用策略由GenerationType定义:

public enum GenerationType { TABLE, SEQUENCE, IDENTITY, AUTO };

最常见的使用方法是指定SEQUENCEIDENTITY

  • 对于使用SEQUENCE的数据库,例如Oracle和HSQLDB,您还必须使用SequenceGenerator注释:

    公共类MyClass{

    <前><代码>@Id
    @GenerateValue(策略=GenerationType.SEQUENCE, 生成器=“SEQMYCLASSID”)
    @SequenceGenerator(名称=“SEQMYCLASSID”,序列名称=“SEQMYCLASSID”)
    私人长ID;

    }

如果您启用了自动 DDL,则不必创建序列,因为 JPA 提供程序会这样做给你的。如果不是这种情况,您必须像这样手动执行此操作:

CREATE SEQUENCE SEQMYCLASSID;
  • 对于不使用序列并使用标识列(例如 Microsoft SQL Server)的数据库,您需要指定IDENTITY作为策略:< /p>

    公共类MyClass{

    <前><代码>@Id
    @GenerateValue(策略=GenerationType.IDENTITY)
    私人长ID;

    }

In order to use GeneratedValue you have to specify the strategy and the generator. By default the strategy is AUTO and the generator is empty.

The avaliable strategies are defined by GenerationType:

public enum GenerationType { TABLE, SEQUENCE, IDENTITY, AUTO };

The most common way to use it is by specifying SEQUENCE or IDENTITY.

  • For databases that use SEQUENCE such as Oracle and HSQLDB you also have to use SequenceGenerator annotation:

    public class MyClass {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQMYCLASSID")
    @SequenceGenerator(name="SEQMYCLASSID", sequenceName="SEQMYCLASSID")
    private Long id;
    

    }

If you have automatic DDL enable you don't have to create the sequence because the JPA provider will do it for you. If that's not the case you have to do it manually like this:

CREATE SEQUENCE SEQMYCLASSID;
  • For databases that don't use squences and use identity columns such as Microsoft SQL Server you need to specify IDENTITY as strategy:

    public class MyClass {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)  
    private Long id;
    

    }

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