[英]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.