简体   繁体   English

问题 java @GeneratedValue (strategy = GenerationType.IDENTITY) 使用 MariaDB 10.4 和 eclipselink

[英]Problems with java @GeneratedValue (strategy = GenerationType.IDENTITY) using MariaDB 10.4 and eclipselink

I am developing a REST web service in Java EE I am using: Glassfish 5.0 (build 25), MariaDB 10.4 and eclipselink (JPA 2.1) here is my code:我正在 Java EE 中开发 REST web 服务 我正在使用:Glassfish 5.0(内部版本 25)、MariaDB 10.4 和 eclipselink(JPA 2.1) 这是我的代码:

commande_line table命令行表

CREATE TABLE IF NOT EXISTS `cooldb`.`commande_line` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `quantity` INT NULL,
  `discount` INT NULL,
  `dish` INT NOT NULL,
  `commande` INT NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `id_UNIQUE` (`id` ASC),
  INDEX `fk_commande_line_dish1_idx` (`dish` ASC),
  INDEX `fk_commande_line_commande1_idx` (`commande` ASC),
  CONSTRAINT `fk_commande_line_dish1`
    FOREIGN KEY (`dish`)
    REFERENCES `cooldb`.`dish` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `fk_commande_line_commande1`
    FOREIGN KEY (`commande`)
    REFERENCES `cooldb`.`commande` (`id`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

persistance.xml持久性.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
  <!-- Define Persistence Unit -->
  <persistence-unit name="my_persistence_unit" transaction-type="JTA">
    <jta-data-source>jdbc/mariadb</jta-data-source>
    <class>com.yac.model.Address</class>
    <class>com.yac.model.Commande</class>
    <class>com.yac.model.CommandeLine</class>
    <class>com.yac.model.Dish</class>
    <class>com.yac.model.Dishtype</class>
    <class>com.yac.model.Ingredient</class>
    <class>com.yac.model.Payement</class>
    <class>com.yac.model.Profil</class>
    <class>com.yac.model.Restaurant</class>
    <class>com.yac.model.Userapp</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
    </properties>
  </persistence-unit>
</persistence>

commandeline entity命令实体

public class CommandeLine implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Column(name = "quantity")
    private Integer quantity;
    @Column(name = "discount")
    private Integer discount;
    @JoinColumn(name = "commande", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Commande commande;
    @JoinColumn(name = "dish", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Dish dish;
    //Constructor
    // Setter and Getter
}

commandeline web service命令行 web 服务

@Stateless
@Path("commandeline")
public class CommandeLineFacadeREST extends AbstractFacade<CommandeLine> {

    @PersistenceContext(unitName = "my_persistence_unit")
    private EntityManager em;

    public CommandeLineFacadeREST() {
        super(CommandeLine.class);
    }

    @POST
    @Override
    @Consumes(MediaType.APPLICATION_JSON)
    public void create(CommandeLine entity) {
        super.create(entity);
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
}

AbstractFacade抽象立面

public abstract class AbstractFacade<T> {

    private Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public void create(T entity) {
        getEntityManager().persist(entity);
    }
}

The problem is when I test my web service with Postman and I try to insert a record with a POST request here is what I receive as error message:问题是当我用 Postman 测试我的 web 服务时,我尝试插入一条带有 POST 请求的记录,这是我收到的错误消息:

Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: (conn=158) Table 'cooldb.sequence' doesn't exist
Error Code: 1146
Call: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?
    bind => [2 parameters bound]
Query: DataModifyQuery(name="SEQ_GEN_SEQUENCE" sql="UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?") ...

I don't understand why the problem with SEQUANCE when I use @GeneratedValue (strategy = GenerationType.IDENTITY).我不明白为什么当我使用 @GeneratedValue (strategy = GenerationType.IDENTITY) 时 SEQUANCE 会出现问题。 When I change with @GeneratedValue (strategy = GenerationType.SEQUENCE) and I create the table with the following script:当我更改为 @GeneratedValue (strategy = GenerationType.SEQUENCE) 并使用以下脚本创建表时:

CREATE SEQUENCE SEQUANCE START WITH 1 INCREMENT BY 1;

by applying the solution shown in: Table 'customerjpa.sequence' doesn't exist JPA but the same probleme通过应用以下所示的解决方案: 表 'customerjpa.sequence' 不存在 JPA但同样的问题

thank you in advance for your help.预先感谢您的帮助。

The problem is solved using Chris comments, i just add the following line in my persistence.xml file:使用 Chris 注释解决了问题,我只需在我的 persistence.xml 文件中添加以下行:

<property name="eclipselink.target-database" value="MySQL"/>

Thank you very much Chris.非常感谢克里斯。 So my new persistence.xml file is:所以我的新persistence.xml文件是:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
    <!-- Define Persistence Unit -->
    <persistence-unit name="my_persistence_unit" transaction-type="JTA">
        <jta-data-source>jdbc/mariadb</jta-data-source>
        <class>com.yac.model.Address</class>
        <class>com.yac.model.Commande</class>
        <class>com.yac.model.CommandeLine</class>
        <class>com.yac.model.Dish</class>
        <class>com.yac.model.Dishtype</class>
        <class>com.yac.model.Ingredient</class>
        <class>com.yac.model.Payement</class>
        <class>com.yac.model.Profil</class>
        <class>com.yac.model.Restaurant</class>
        <class>com.yac.model.Userapp</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.target-database" value="MySQL"/>
        </properties>
    </persistence-unit>
</persistence>

I just specified the database platform in MySQL in the persistence.xml file of the moment MariaDB is based on it, because MariaDB is not mentioned in the list. I just specified the database platform in MySQL in the persistence.xml file of the moment MariaDB is based on it, because MariaDB is not mentioned in the list. If there are other suggestions do not hesitate thank you.如果有其他建议,请不要犹豫,谢谢。

Another Solution: Add ?useMysqlMetadata=true to your JDBC URL connection as bellow:另一个解决方案:将?useMysqlMetadata=true添加到您的 JDBC URL 连接中,如下所示:

<property name="URL" value="jdbc:mariadb://[HOST]:[PORT]/[DB NAME]?useMysqlMetadata=true"/>

that will make MariaDB use MySQL meta data and then eclipselink will detect it as MySQL.这将使 MariaDB 使用 MySQL 元数据,然后 eclipselink 会将其检测为 MySQL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot @GeneratedValue(strategy = GenerationType.IDENTITY) 不起作用 - Spring Boot @GeneratedValue(strategy = GenerationType.IDENTITY) not working 在填充的数据库中使用@GeneratedValue(strategy = GenerationType.IDENTITY)会导致PK约束错误 - Using @GeneratedValue(strategy=GenerationType.IDENTITY) in populated database causes PK constraint error 注释@Id 和@GeneratedValue(strategy = GenerationType.IDENTITY) 有什么用? 为什么世代类型是身份? - what is the use of annotations @Id and @GeneratedValue(strategy = GenerationType.IDENTITY)? Why the generationtype is identity? 为什么在使用策略GenerationType.IDENTITY时Hibernate尝试访问hibernate_sequence? - Why is Hibernate trying to access hibernate_sequence when using strategy GenerationType.IDENTITY? 休眠:@GeneratedValue(strategy = GenerationType - Hibernate: @GeneratedValue(strategy = GenerationType 在为GenerationType.IDENTITY提供标识符值作为生成标识符的策略时,传递给分离的实体 - Detached entity passed to persist when providing identifier value for GenerationType.IDENTITY as strategy to generate the identifier Hibernate GenerationType.IDENTITY 不生成序列 ID - Hibernate GenerationType.IDENTITY not generating sequence ids @ GenerationType.IDENTITY失败10以上 - @GenerationType.IDENTITY failing above 10 全局配置Hibernate以使用GenerationType.IDENTITY - Configure Hibernate globally to use GenerationType.IDENTITY Hibernate 1.0 + JPA + Netbeans 7.01 + SQL Server 2005 =使用GenerationType.IDENTITY插入表中的问题 - Hibernate 1.0 + JPA + Netbeans 7.01 + Sql server 2005 = Problems to insert into table with GenerationType.IDENTITY
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM