Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

I have a problem using MapStruct. I created a record that has an id of type long and in my mapper I need to convert this object to the Long type, but the error below occurs. I will leave the code to make the problem clearer, sorry if any information is missing, it's my first question on the forum.

Ambiguous constructors found for creating java.lang.Long: Long(long), Long(java.lang.String). Either declare parameterless constructor or annotate the default constructor with an annotation named @default.

My code:

//IdClient.java

public record IdClient(Long id) { }
//ClientEntity.java

@Table(name = "client") @Builder 
public record ClientEntity(Long idClient){} 
//ClientMapper.java

@ApplicationScoped public interface ClientMapper { 

  ClientEntity IdClientToClientEntity(IdClient id);

  Long map(IdClient id);

}

I solved it, @Mapping(target = "idClient", source = "idClient.id") but I'm not sure if this is the most appropriate way, considering that the record is of type long and my entity expects a long. I opened this topic to find out what would be the best approach for this case.

You must be logged in to vote

Replies: 1 comment · 1 reply

Comment options

@Mapping(target = "idClient", source = "idClient.id") is the correct solution.
if your records would be:

public record IdClient(Long id) { }
public record ClientEntity(Long id){} 

The Mapper would map automatically without you specifying the Mapping via @Mapping

See: Basic Mapping

When a property has the same name as its target entity counterpart, it will be mapped implicitly.
When a property has a different name in the target entity, its name can be specified via the @mapping annotation.

You must be logged in to vote
1 reply
@RodrigoMonterio
Comment options

Understood, thank you for your explanation @zyberzebra

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.