spring-data repository with pre-existing or generated DTO

Viewed 114

Let's say I want to create a repository like this

@Repository
public interface UserRepository extends CrudRepository<User, Long> {...}

but User is a class that I am getting from somewhere else, so I can not annotate it like it's supposed to be annotated:

@Entity
@AllArgsConstructor
@NoArgsConstructor
public class User {

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Long id;

   private String name;
}

In my case, I am working with Protobuf objects, but this "issue" will be there for any other existing POJO that can not be modified (e.g. org.springframework.social.twitter.api.Tweet)

All I can think is to do the mapping through orm.xml file.

Is there another way to inject the needed annotations to the existing POJO?

@Repository
public interface UserRepository extends CrudRepository<@Entity @Id User, Long> {...}

Or maybe by default, the POJO used in the repository should take by default these annotations if there is a field Long id suitable for it...

Another workaround is creating a new POJO which has the same fields or the needed ones to be stored in the DB, and transfer the content from the generated one.

0 Answers
Related