MongoRepository custom Id

15 views
Skip to first unread message

Manuel Doncel Martos

unread,
Oct 20, 2016, 3:33:31 PM10/20/16
to mongodb-user

Hi to all,


This is my first personal (and no personal) project with MongoDB and I have one doubt that you all may have a solution for it.


I have the following MongoDB Repository


public interface TeamRepository extends MongoRepository<Team, TeamId> {
    ....
}


And the following classes:


public abstract class DbId implements Serializable {
    @Id
    private final String id;

    public DbId(final String id) { this.id = id;}

    public String getId() { return id;}
}

public class TeamId extends DbId {

    public TeamId(final String id) {
        super(id);
    }
}


As you can see, I have a custom id for the repository (I have MongoRepository<Team, TeamId> instead of something like MongoRepository<Team, String>). The TeamId class, only contains a String id. 


When I am trying to save a Team object, with teamRepository.save(team), I get an error saying that MongoDB does not know how to generate TeamId. Any clue?

Reply all
Reply to author
Forward
0 new messages