xHow can I have @Id string for CrudRepository in Spring with Spring Data JPA?
Asked Answered
C

5

6

The problem is that I am getting an exception using @RepositoryRestResource for my UserRepository that extends JpaRepository.

The reason for that is that findById is only accepting Long or Int types by default, even I have

@Id String id; and not @Id Int id in my entity definition.

I have tried searching StackOverflow and Google, but haven't found any solutions.

The error message is as follows:

"Failed to convert from type [java.lang.String] to type [java.lang.Integer] for value '3175433272470683'; nested exception is java.lang.NumberFormatException: For input string: \"3175433272470683\""

I want to make it work with a

@Id String id;

Any suggestions?

Many thanks in advances. It's a big privilege to ask questions here.

The Entity class:

@Entity // This tells Hibernate to make a table out of this class
@Table(name = "users")
public class XmppUser {
    @Id
    private java.lang.String username;

    private String password;
    private String serverkey;
    private String salt;
    private int iterationcount;
    private Date created_at;

    //    @Formula("ST_ASTEXT(coordinates)")
//    @Column(columnDefinition = "geometry")
//    private Point coordinates;
    //    private Point coordinates;
    private String full_name;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "username", nullable = true)
    private XmppLast xmppLast;
Chromatogram answered 10/8, 2019 at 1:2 Comment(2)
Could you post the entity class code?Overshoe
Just posted an entity classChromatogram
C
11

You must change the type of the ID type parameter in the repository to match with the id attribute type on your entity.

From the Spring docs:

Interface Repository<T,ID>
Type Parameters:
  T - the domain type the repository manages    
  ID - the type of the id of the entity the repository manages

Based on

@Entity // This tells Hibernate to make a table out of this class
@Table(name = "users")
public class XmppUser {
    @Id
    private java.lang.String username;
    //...

    }

It should be

public interface UserRepository extends CrudRepository<XmppUser, String> {
    //..
    }

See:

Clevey answered 10/8, 2019 at 1:26 Comment(5)
Just added this to the repository: @Override Optional<XmppUser> findById(String integer);Chromatogram
It says: method does not override method from its superclassChromatogram
@DavidJones , I’ve added an exampleClevey
@admiz635 that's exactly what I have, and inside it: @Override Optional<XmppUser> findById(String integer);Chromatogram
Oh, my bad. Just noticed that the second type parameter to CrudRepository<XmppUser, String> is a String! That solved the problem! Great job @admiz635!Chromatogram
L
1

JpaRepository is a special case of CrudRepository. Both JpaRepository and CrudRepository declare two type parameters, T and ID. You will need to supply these two class types. For example,

public interface UserRepository extends CrudRepository<XmppUser, java.lang.String> {
//..
}

or

public interface UserRepository extends JpaRepository<XmppUser, java.lang.String> {
//..
}

Notice that the second type java.lang.String must match the type of the primary key attribute. In this case, you cannot specify it as String or Integer, but it is java.lang.String instead.

Try not to name a customized class as String. It is a bad practice to use the same class name as already present in the JDK.

Legislative answered 10/3, 2021 at 0:9 Comment(0)
O
0

You could try something like this:

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(name = "PR_KEY")
private String prKey;

If you want to read more about this subject you could begin looking throw here or here

Overshoe answered 10/8, 2019 at 1:21 Comment(1)
Tried. It doesn't work. Same error. Just tried again.Chromatogram
C
0

according to the latest version of spring data jpa(2.1.10 GA), you can use it like enter image description here

here is the link

Congener answered 10/8, 2019 at 1:27 Comment(4)
@DavidJones so your spring data jpa version is?Congener
Version: 2.1.9.RELEASEChromatogram
@DavidJones try it like this:@Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private String id;Congener
Doesn't solve the problem. I expected Spring to have fewer issues. That's the second bug I found today.Chromatogram
T
0

I think there is an approach to solve this problem.

Let's say, Site is our @Entity.

@Id
private String id;

getters setters

then you can invoke findById as follow

 Optional<Site> site = getSite(id);

Note: this worked for me, I hope it will help someone.

Tan answered 15/11, 2021 at 5:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.