1

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. 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

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.

So the generated code by spring roo in the test_Roo_Jpa_Entity.aj is:

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. But I'm not sure how to use these commands.

Thank you very much for your help in advance!

EDIT 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()
Community
  • 1
  • 1
Lars
  • 11
  • 1
  • Does the answer helps? Or do you have a solution? – Patrick Jan 05 '16 at 13:53
  • Hey Patrick. Thanks for your answer. I tried your suggestion, but it looks like that it doesn't solve my problem. In my mysql database I still have auto-generated ids in the id column. – Lars Jan 05 '16 at 16:16
  • can you show the code where you set the Id's manually. – Patrick Jan 05 '16 at 16:44
  • Sure. I imported the corresponding *-classes.jar file in my code and read the data from a text, where the first column is the id and the second column is the name. The code itself is: `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();` – Lars Jan 06 '16 at 07:57
  • Thanks, can you try it again by removing this line completly: `@GeneratedValue(strategy = GenerationType.AUTO)`. I tested it with an inMemory DB with Spring and it works. – Patrick Jan 07 '16 at 07:00

1 Answers1

1

I think you have to move this part from your aspectJ file to your java Entity file.

@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; and right click. Then you have to click on AspectJ Refactoring and Push In. This part should now be moved in your Java class.

Then change the strategy to @GeneratedValue(strategy = GenerationType.IDENTITY)

Now you should be able to create your own Id's. Sure your Database should also accept this.

Patrick
  • 12,336
  • 15
  • 73
  • 115