5

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;
2
  • Could you post the entity class code? Aug 10, 2019 at 1:11
  • Just posted an entity class Aug 10, 2019 at 1:17

5 Answers 5

10

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:

5
  • Just added this to the repository: @Override Optional<XmppUser> findById(String integer); Aug 10, 2019 at 1:33
  • It says: method does not override method from its superclass Aug 10, 2019 at 1:33
  • @DavidJones , I’ve added an example
    – admlz635
    Aug 10, 2019 at 1:35
  • @admiz635 that's exactly what I have, and inside it: @Override Optional<XmppUser> findById(String integer); Aug 10, 2019 at 1:38
  • Oh, my bad. Just noticed that the second type parameter to CrudRepository<XmppUser, String> is a String! That solved the problem! Great job @admiz635! Aug 10, 2019 at 2:48
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

1
  • Tried. It doesn't work. Same error. Just tried again. Aug 10, 2019 at 1:23
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

4
  • @DavidJones so your spring data jpa version is?
    – Stone Feng
    Aug 10, 2019 at 1:38
  • Version: 2.1.9.RELEASE Aug 10, 2019 at 1:41
  • @DavidJones try it like this:@Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private String id;
    – Stone Feng
    Aug 10, 2019 at 1:46
  • Doesn't solve the problem. I expected Spring to have fewer issues. That's the second bug I found today. Aug 10, 2019 at 2:19
0

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.