@IdPrefix (and other similar) annotations not working at all

java

#1

I saw a sample usage of @IdPrefix (and other similar) here. I am trying to store a document in the DB but somehow the key is still without prefix (or suffixes that i use!)

Unlike in the above link, I am using ReactiveCouchbaseRepository to save the doc (rather than template). The following is the relevant code sample.

the model class

@Document
@Data
@EqualsAndHashCode(of = {"id"})
public final class User {

private static final String DOC_TYPE = "user";

@Id
@GeneratedValue(strategy = GenerationStrategy.USE_ATTRIBUTES, delimiter = "::")
@NotBlank
private final String id;

@IdPrefix
private String prefix = DOC_TYPE;

}

the test class

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTest {

private static final User TEST_USER = User.createNew(Status.NEW, asList(new Profile(ProfileName.DOCTOR, Status.NEW)));

@Autowired
UserApplication.UserRepository userRepository;

@Test
public void shouldSaveUser() throws ParseException {
    final User user = User.createNew(/*...populate values, including id.../*);
    userRepository.save(user).subscribe();

    StepVerifier.create(userRepository.findById(user.getId()))
            .expectNext(user)
            .verifyComplete();
}

Here, post this method is executed, the doc stored does not have the prefix in the doc stored in the db. Any help would be appreciated. Thanks in advance.


#2

@nilesh_ca, did this work for you ? I am also facing same issue . Could you please help me on this, if this has worked for you ?


#3

Hi @lokendra1985rawat, which version of SDC are you using? Is there a public getter method for the field?


#4

Hi @subhashni, I am using couchbase 6.0 enterprise version and is integrated with springboot 2.0, so i guess it would be latest version.


#5

Here is an example, if the field is going to be still a part of the document, use @IdAttribute, and it is required that the id field is not set, if it is set by code, then that will take precedence,

@Document
public class User {

	@Id @GeneratedValue(strategy = USE_ATTRIBUTES, delimiter = "::")
	private String id;

	@IdAttribute(order = 0)
	private String firstName;

	@IdAttribute(order = 1)
	private String lastName;

	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getFirstName() {
		return this.firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return this.lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
}


User user = new User();
user.setFirstName("Alice");
user.setLastName("Smith");.userRepository.save(user);

the result document will have id Alice::Smith