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;

5 Answers5

9

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:

admlz635
  • 1,001
  • 1
  • 9
  • 18
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

Sham Fiorin
  • 403
  • 4
  • 16
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

Stone Feng
  • 69
  • 1
  • 3
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.

justthink
  • 439
  • 3
  • 6
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.