Id is null when retrieving with Spring Data

java

#1

I have created a sample project to illustrate the problem: https://github.com/corneil/spring-data-couchbase-demo

The following method fails:

@Test
	public void testBasicSave() throws ParseException {
		UserInfo user = new UserInfo();
		user.setUserId("joe");
		user.setDateOfBirth(makeDate("1999-11-11"));
		user.setEmailAddress("joe@soap.com");
		user.setFullName("Joe Soap");
		userInfoRepository.save(user);
		assertNotNull(user.getId());
	}

    @Document
    @Data
    @EqualsAndHashCode(of = {"userId"})
    public class UserInfo {
    	@Id
    	@GeneratedValue(strategy = GenerationStrategy.UNIQUE)
    	String id;

    	@Field
    	@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    	Date dateOfBirth;

    	@Field
    	String emailAddress;

    	@NotNull
    	@Field
    	String fullName;

    	@NotNull
    	@Field
    	String userId;

    	public UserInfo() {
    		super();
    	}

    	public UserInfo(String userId, String fullName) {
    		this.userId = userId;
    		this.fullName = fullName;
    	}
    }

@ViewIndexed(designDoc = "userInfo")
public interface UserInfoRepository extends CouchbaseRepository<UserInfo, String> {
	UserInfo findOneByUserId(String userId);
}

#2

@corneil did you create the right views/n1ql indexes when searching by user id? only the findById is key/value since otherwise we need a secondary index to look up the document.


#3

The repositories are annotated with ViewIndexed and the view does exist
after application starts running.

That doesn’t explain how the id is null after creating an instance.

[image: --]
Corneil du Plessis
[image: https://]about.me/corneil
https://about.me/corneil?promo=email_sig


#4

Hi @corneil,

Query derived for the method findOneByUserId would require N1ql indexes. Are you able to retrieve the other contents of UserInfo where only Id is null?


#5

If you look at the little sample project I linked to there are tests in

I haven’t tried a fineOne using an id.

testFindAll and testFind pass the tests.
testBasicSave fails with id null after save
id has value when using findOneByUserId
testSaveAndGroupUser print the following:
Basically, it means that id is null after save, Object of classes annotated
as @Document are embedded and not ‘linked’

2017-07-13 15:38:56.987 ERROR 6436 — [ main]
c.g.c.s.c.demo.test.BasicUserTest : userInfo.id is null
2017-07-13 15:38:57.124 ERROR 6436 — [ main]
c.g.c.s.c.demo.test.BasicUserTest : groupInfo.id is null
2017-07-13 15:38:57.124 INFO 6436 — [ main]
c.g.c.s.c.demo.test.BasicUserTest : groupInfo:saved:null
2017-07-13 15:38:57.280 ERROR 6436 — [ main]
c.g.c.s.c.demo.test.BasicUserTest : member.id is null
2017-07-13 15:38:57.488 INFO 6436 — [ main]
c.g.c.s.c.demo.test.BasicUserTest :
member:GroupMember(id=ad75b2b4-cf3f-4ef8-8239-fb2f1e0fd964, enabled=true,
member=UserInfo(id=null, dateOfBirth=Thu Nov 11 00:00:00 EET 1999,
emailAddress=joe@soap.com, fullName=Joe Soap, userId=joe),
memberOfgroup=GroupInfo(id=null, groupName=group1,
groupOwner=UserInfo(id=null, dateOfBirth=Thu Nov 11 00:00:00 EET 1999,
emailAddress=joe@soap.com, fullName=Joe Soap, userId=joe)))
2017-07-13 15:38:57.488 ERROR 6436 — [ main]
c.g.c.s.c.demo.test.BasicUserTest : groupMember.member.id is null
2017-07-13 15:38:57.488 ERROR 6436 — [ main]
c.g.c.s.c.demo.test.BasicUserTest : groupMember.memberOfgroup.id is
null

[image: --]
Corneil du Plessis
[image: https://]about.me/corneil
https://about.me/corneil?promo=email_sig


#6

I just added a testFindOne where I use UUID.randomUUID() to generate an ID
and the find works and the id has a value after reading.

[image: --]
Corneil du Plessis
[image: https://]about.me/corneil
https://about.me/corneil?promo=email_sig


#7

Hi @corneil, is it confirmed that it’s still an issue of spring-data-couchbase that save(…) method returns entity with null id
(@Id with @GeneratedValue(strategy = GenerationStrategy.UNIQUE))

cause I’m facing the same issue. To work around I have to explicitly set id before saving


#8

Hi @hoahohart and @corneil,

The unique id was indeed used for saving the document, however there was a bug in updating the id on the saved entity returned. This is now fixed in current master, it will be available in the next release.

Thanks.