Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I provide my own @id field using Spring Roo and JPA

I am trying to get Spring Roo to use my own @Id field instead of generating one.

@Entity
...
@RooEntity
@Table(name = "usr")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "usr_id")
    private Integer id;
    ...
    public Integer getId() { return id; }
    public void setId(Integer id) { this.id = id }
    ...
}

Roo still creates the following in User_Roo_Entity.aj:

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

How can I get it to acknowledge my @Id field? I want to specify my own generator etc.

like image 359
David Tinker Avatar asked Nov 19 '10 16:11

David Tinker


People also ask

Is @ID mandatory in JPA?

Id is required by JPA, but it is not required that the Id specified in your mapping match the Id in your database. For instance you can map a table with no id to a jpa entity. To do it just specify that the "Jpa Id" is the combination of all columns.

What is @ID annotation in JPA?

@Id annotation is the JPA is used for making specific variable primary key.

How to get generated ID in JPA?

The most straightforward way is to define a generated ID in a JPA entity is to annotate a field with the @Id and @GeneratedValue annotations. We don't even need to specify any parameters for the @GeneratedValue .

What is Spring Data JPA?

Spring Data JPA aims to significantly improve the implementation of data access layers by reducing the effort to the amount that's actually needed. As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically.


1 Answers

Looks like this is a bug in Spring Roo 1.1.0.RELEASE. I changed @Id to @javax.persistence.Id and it works. Explicitly importing javax.persistence.Id also works (instead of just javax.persistence.*). I have optimize imports on in IntelliJ so the first option is probably the best workaround.

like image 148
David Tinker Avatar answered Oct 02 '22 17:10

David Tinker