Skip to content

NullPointerException for embbedded objects #1529

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
sergey-morenets opened this issue Jun 12, 2023 · 2 comments
Closed

NullPointerException for embbedded objects #1529

sergey-morenets opened this issue Jun 12, 2023 · 2 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@sergey-morenets
Copy link

Hi

We have the following Java record:

public record Point (int x, int y) {

	public static Point valueOf(String text) {
		return null;
	}
}

And most often store it using converter:

@WritingConverter
public class PointToStringConveter implements
	Converter<Point, String>{
	
	@Override
	public String convert(Point point) {
		return point.toString();
	}
}

However for one entity we would like to store it as embedded object:

	@Embedded(onEmpty = OnEmpty.USE_NULL)
	private Point location;

Unfortunately in this case we receive the following exception while saving it:

Caused by: java.lang.NullPointerException: Cannot invoke "org.springframework.data.relational.core.mapping.RelationalPersistentEntity.getPropertyAccessor(Object)" because "persistentEntity" is null
	at org.springframework.data.jdbc.core.convert.SqlParametersFactory.getParameterSource(SqlParametersFactory.java:244)
	at org.springframework.data.jdbc.core.convert.SqlParametersFactory.lambda$getParameterSource$2(SqlParametersFactory.java:260)
	at org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:298)
	at org.springframework.data.mapping.PersistentEntity.doWithAll(PersistentEntity.java:297)
	at org.springframework.data.jdbc.core.convert.SqlParametersFactory.getParameterSource(SqlParametersFactory.java:247)
	at org.springframework.data.jdbc.core.convert.SqlParametersFactory.forInsert(SqlParametersFactory.java:83)
	at org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy.insert(DefaultDataAccessStrategy.java:104)
	at org.springframework.data.jdbc.core.JdbcAggregateChangeExecutionContext.executeInsertRoot(JdbcAggregateChangeExecutionContext.java:83)
	at org.springframework.data.jdbc.core.AggregateChangeExecutor.execute(AggregateChangeExecutor.java:85)

Spring Data JDBC: 3.1.0

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 12, 2023
@schauder
Copy link
Contributor

You can either handle a class as an entity which might be embedded or as a type mapped to a single column, which you do by using a converter.

You can't mix the two.

As a workaround you could create separate subclasses to use in the two different settings.

@schauder schauder closed this as not planned Won't fix, can't repro, duplicate, stale Jun 12, 2023
@schauder schauder added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 12, 2023
@sergey-morenets
Copy link
Author

Hi @schauder

Can you please mention in the documentation that entity used in the converter can't be applied as embedded?
Because the raised exception is confusing and doesn't explain the cause of this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants