简体   繁体   English

Spring Roo-在MySQL数据库中使用自己的ID而不是自动生成的密钥

[英]spring roo - using own id instead of auto-generated key in mysql database

I'm new to spring-roo and I want to generate and use my on ids for database entries in a mysql database instead of the auto-generated ids. 我是spring-roo的新手,我想针对MySQL数据库中的数据库条目生成并使用on ID代替自动生成的ID。 And I'm not sure how to do this. 而且我不确定该怎么做。 The only related post I found here is this one: 我在这里找到的唯一相关文章是:

How can I provide my own @id field using Spring Roo and JPA 如何使用Spring Roo和JPA提供自己的@id字段

But this post is now four years old and I'm using spring roo 1.3.2 instead of 1.1.0 so maybe something changed in the meantime. 但是这篇文章现在已经四岁了,我使用的是Spring Roo 1.3.2而不是1.1.0,所以在此期间可能有所改变。

So the generated code by spring roo in the test_Roo_Jpa_Entity.aj is: 因此,spring roo在test_Roo_Jpa_Entity.aj中生成的代码为:

privileged aspect test_Roo_Jpa_Entity {

declare @type: test: @Entity;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long test.id;

This is also what I see in the database. 这也是我在数据库中看到的。 The id of a new entry is autogenerated and this is something I want to avoid.I played around with the --identifierField or --identifierColumn commands in spring roo but so far without any success. 新条目的id是自动生成的,这是我要避免的事情。我在spring roo中玩过--identifierField或--identifierColumn命令,但到目前为止没有成功。 But I'm not sure how to use these commands. 但是我不确定如何使用这些命令。

Thank you very much for your help in advance! 非常感谢您的提前帮助!

EDIT 1: 编辑1:

Code for persit my Entity: 允许我访问实体的代码:

 String[] split = line.split(";"); Long TEST_ID = Long.valueOf(split[0]);
 String TEST_NAME = split[1]; TEST test = new TEST(); test.setName(TEST_NAME);
 test.setId(TEST_ID); test.persist()

I think you have to move this part from your aspectJ file to your java Entity file. 我认为您必须将这一部分从AspectJ文件移动到Java Entity文件。

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long test.id;

To do so, you have to mark the line private Long test.id; 为此,您必须将该行标记为private Long test.id; and right click. 然后右键单击。 Then you have to click on AspectJ Refactoring and Push In . 然后,您必须单击AspectJ Refactoring推入 This part should now be moved in your Java class. 现在应在Java类中移动此部分。

Then change the strategy to @GeneratedValue(strategy = GenerationType.IDENTITY) 然后将策略更改为@GeneratedValue(strategy = GenerationType.IDENTITY)

Now you should be able to create your own Id's. 现在,您应该可以创建自己的ID了。 Sure your Database should also accept this. 确保您的数据库也应该接受这一点。

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

相关问题 MySQL-外键和自动生成的ID - MySQL - Foreign key and auto-generated ID 如何在Spring中仅使用自动生成的值将行插入DB,并返回ID? - How to insert a row to DB in spring, using only auto-generated values, and return an ID? 从Oracle数据库检索Java代码中的自动生成的ID - Retrieve Auto-Generated ID in Java Code From Oracle DataBase Google App Engine(Java)数据库自动生成的ID - Google App Engine(Java) database auto-generated ID Spring 3.2使用SimpleJdbcInsert检索自动生成的密钥 - Spring 3.2 retrieving auto-generated keys using SimpleJdbcInsert 使用自动生成的密钥从JDBC Derby数据库中删除(始终以IDEGER身份生成始终为INTEGER NOT NULL(以1开头,以1递增)) - Delete from JDBC Derby Database using the auto-generated key (INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)) 使用JPA和自动生成的主键在数据库中创建条目 - Creating entries in a database using JPA and auto-generated primary keys 如何将 Springdoc Swagger UI 指向我自己的 YAML 文件而不是自动生成的文件? - How to point Springdoc Swagger UI to my own YAML file instead of the auto-generated one? 如何在PreparedStatement中使用自动生成的@Id? - How to use auto-generated @Id in PreparedStatement? Java:检查自动生成的 ID - Java: Check auto-generated ID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM