Skip to content

Field Mapping #1068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
haorancui94 opened this issue Jan 26, 2021 · 2 comments · Fixed by #1186
Closed

Field Mapping #1068

haorancui94 opened this issue Jan 26, 2021 · 2 comments · Fixed by #1186
Labels
type: enhancement A general enhancement

Comments

@haorancui94
Copy link

I want my userId in code to be user_id field in document.
I was using the field to converting the field with
@Field(name = "user_id")
but it won't work

instead if I use:
@Field(value = "user_id")
it will work

I know in the doc it told us to use @Field("user_id")

But I check the code, might be the code has written in the opposite way that caused this problem?

         /**
	 * The key to be used to store the field inside the document. Alias for {@link #name()}.
	 *
	 * @return an empty {@link String} by default.
	 */
	@AliasFor("name")
	String value() default "";

	/**
	 * The key to be used to store the field inside the document. Alias for {@link #value()}.
	 *
	 * @return an empty {@link String} by default.
	 */
	@AliasFor("value")
	String name() default "";
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 26, 2021
@mikereiche
Copy link
Collaborator

mikereiche commented Feb 4, 2021

This does look a little weird and behaves a little weird but it's the same as at least one other spring-data implementation.
I'll see if I can make all three forms do the same thing.

@mikereiche mikereiche added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 4, 2021
@mikereiche
Copy link
Collaborator

This change to BasicCouchbasePersistentProperty would change the usage of the Field annotation as desired:

/**
 * Returns the field name of the property.
 * <p/>
 * The field name can be different from the actual property name by using a custom annotation.
 */
String fieldName;
@Override
public String getFieldName() {
	if(fieldName != null ){
		return fieldName;
	}
	Field annotationField = getField().getAnnotation(Field.class);

	if (annotationField != null) {
		if (StringUtils.hasText(annotationField.value())) {
			return fieldName = annotationField.value();
		} else if (StringUtils.hasText(annotationField.name())) {
			return fieldName = annotationField.name();
		}
	}
	JsonProperty annotation = getField().getAnnotation(JsonProperty.class);

	if (annotation != null && StringUtils.hasText(annotation.value())) {
		return fieldName = annotation.value();
	}

	String fName = fieldNamingStrategy.getFieldName(this);

	if (!StringUtils.hasText(fName)) {
		throw new MappingException(String.format("Invalid (null or empty) field name returned for property %s by %s!",
				this, fieldNamingStrategy.getClass()));
	}

	return fieldName = fName;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
3 participants