Id is null when retrieving with Spring Data

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);
}

@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.

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

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?

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

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

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

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.

1 Like

The problem is resolved for the top level entity but it looks like it still exists for the nested elements.

e.g. if the entity is like (in kotlin) data class TestEntity(@IdAttribute id: String?, name: String?, gender: String?, children: List<TestEntity>), then,

  1. id is being read properly for the top level object
  2. id for all the children are returned with their respective ids populated as null
  3. the remaining attributes - name, gender, are populated correctly for both top level object and all the children

Note:

  1. in my case, I am using GeneratedValue(strategy = GenerationStrategy.USE_ATTRIBUTES with the other appropriate settings; so the document key in the database should be the configured prefix + id
  2. if we remove @IdAttribute annotation from id, then the above problem is solved. However, of course, as expected, the document key (in the database) is generated with only the prefix value, i.e. the id is not appended to the prefix value in the document key.