Skip to content

MappingException: Couldn't find PersistentEntity for type class ...domain.User$HibernateProxy #2095

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
godzzo opened this issue Jan 5, 2022 · 4 comments
Assignees
Labels
status: duplicate A duplicate of another issue

Comments

@godzzo
Copy link

godzzo commented Jan 5, 2022

Description

I wrote a custom Controller for dynamic filtering, and try to respond with the result (paged and projected entities), it failed with the lazy loaded property (HibernateProxy - Image.creator) when PersistentEntityJackson2Module$ProjectionSerializer try to serialize it.

It is a working project on spring-boot 2.5.8, and after I changed to 2.6.2 this Exception happened.

MVC Controller

@RequestMapping(value = ["/{repository}/filterImages"], method = [RequestMethod.GET])
fun imagesFilter(filter: ImageFilter,
	pageable: Pageable,
	assembler: PagedResourcesAssembler<ImageProjection>,
	session: HttpSession
) : ResponseEntity<PagedModel<EntityModel<ImageProjection>>> {
	val result = filterService.invoke(filter, session, pageable)

	val projected = result.map {
		factory.createProjection(ImageProjection::class.java, it)
	}

	return ResponseEntity(assembler.toModel(projected), HttpStatus.OK)
}

Entity

@Entity
@EntityListeners(ImageEntityListener::class)
@Table(name = "image")
class Image {
	...
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "creator_id", nullable=true,
		foreignKey = ForeignKey(name = "fk_image_creator")
	)
	override var creator: User? = null
	...
}

Exception

Caused by: org.springframework.data.mapping.MappingException: Couldn't find PersistentEntity for type class ...domain.User$HibernateProxy$sTtGM2R0!
	at org.springframework.data.mapping.context.MappingContext.getRequiredPersistentEntity(MappingContext.java:79) ~[spring-data-commons-2.6.0.jar:2.6.0]
	at org.springframework.data.mapping.context.PersistentEntities.getRequiredPersistentEntity(PersistentEntities.java:115) ~[spring-data-commons-2.6.0.jar:2.6.0]
	at org.springframework.data.rest.core.support.DefaultSelfLinkProvider.entityIdentifierOrNull(DefaultSelfLinkProvider.java:120) ~[spring-data-rest-core-3.6.0.jar:3.6.0]
	at org.springframework.data.rest.core.support.DefaultSelfLinkProvider.getResourceId(DefaultSelfLinkProvider.java:109) ~[spring-data-rest-core-3.6.0.jar:3.6.0]
	at org.springframework.data.rest.core.support.DefaultSelfLinkProvider.createSelfLinkFor(DefaultSelfLinkProvider.java:84) ~[spring-data-rest-core-3.6.0.jar:3.6.0]
	at org.springframework.data.rest.core.support.DefaultSelfLinkProvider.createSelfLinkFor(DefaultSelfLinkProvider.java:74) ~[spring-data-rest-core-3.6.0.jar:3.6.0]
	at org.springframework.data.rest.webmvc.mapping.DefaultLinkCollector.lambda$createSelfLink$1(DefaultLinkCollector.java:129) ~[spring-data-rest-webmvc-3.6.0.jar:3.6.0]
	at java.base/java.util.Optional.orElseGet(Optional.java:369) ~[na:na]
	at org.springframework.data.rest.webmvc.mapping.DefaultLinkCollector.createSelfLink(DefaultLinkCollector.java:129) ~[spring-data-rest-webmvc-3.6.0.jar:3.6.0]
	at org.springframework.data.rest.webmvc.mapping.DefaultLinkCollector.getLinksFor(DefaultLinkCollector.java:95) ~[spring-data-rest-webmvc-3.6.0.jar:3.6.0]
	at org.springframework.data.rest.webmvc.mapping.DefaultLinkCollector.getLinksFor(DefaultLinkCollector.java:79) ~[spring-data-rest-webmvc-3.6.0.jar:3.6.0]
	at org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$ProjectionSerializer.toModel(PersistentEntityJackson2Module.java:728) ~[spring-data-rest-webmvc-3.6.0.jar:3.6.0]
	at org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$ProjectionSerializer.serialize(PersistentEntityJackson2Module.java:693) ~[spring-data-rest-webmvc-3.6.0.jar:3.6.0]
	at org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$ProjectionSerializer.serialize(PersistentEntityJackson2Module.java:650) ~[spring-data-rest-webmvc-3.6.0.jar:3.6.0]
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728) ~[jackson-databind-2.13.1.jar:2.13.1]
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:774) ~[jackson-databind-2.13.1.jar:2.13.1]
	... 123 common frames omitted
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 5, 2022
@darioseidl
Copy link
Contributor

I'm getting the same exception after upgrading to Spring Boot 2.6+, presumably because in 2.6.0 there was an upgrade to Spring Data 2021.1.0.

@mp911de mp911de self-assigned this Jan 10, 2022
@mp911de mp911de added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 10, 2022
@mp911de
Copy link
Member

mp911de commented Jan 10, 2022

We've fixed the issue in Spring Data JPA with spring-projects/spring-data-jpa#2383 but the fix isn't released yet. Can you test against 2.6.1 snapshots of Spring Data JPA?

@mp911de mp911de closed this as completed Jan 10, 2022
@godzzo
Copy link
Author

godzzo commented Jan 10, 2022

We've fixed the issue in Spring Data JPA with spring-projects/spring-data-jpa#2383 but the fix isn't released yet. Can you test against 2.6.1 snapshots of Spring Data JPA?

I have tested the snapshot, works fine. Thank You!

@joao-borges
Copy link

Hello @mp911de I am having this problem and I am on spring-data-jpa 2.6.3. Maybe it's a different flavor of the same issue?

See

Caused by: org.springframework.data.mapping.MappingException: Cannot get or create PersistentEntity for type c.m.m.s.m.User$HibernateProxy$giIeFBAb! PersistentEntities knows about 3 MappingContext instances and therefore cannot identify a single responsible one. Please configure the initialEntitySet through an entity scan using the base package in your configuration to pre initialize contexts.
	at org.springframework.data.mapping.context.PersistentEntities.lambda$getRequiredPersistentEntity$0(PersistentEntities.java:119)
	at java.base/java.util.Optional.orElseThrow(Optional.java:408)
	at org.springframework.data.mapping.context.PersistentEntities.getRequiredPersistentEntity(PersistentEntities.java:118)
	at org.springframework.data.rest.core.support.DefaultSelfLinkProvider.entityIdentifierOrNull(DefaultSelfLinkProvider.java:120)
	at org.springframework.data.rest.core.support.DefaultSelfLinkProvider.getResourceId(DefaultSelfLinkProvider.java:109)
	at org.springframework.data.rest.core.support.DefaultSelfLinkProvider.createSelfLinkFor(DefaultSelfLinkProvider.java:84)
	at org.springframework.data.rest.core.support.DefaultSelfLinkProvider.createSelfLinkFor(DefaultSelfLinkProvider.java:74)
	at org.springframework.data.rest.webmvc.mapping.DefaultLinkCollector.lambda$createSelfLink$1(DefaultLinkCollector.java:129)
	at java.base/java.util.Optional.orElseGet(Optional.java:369)
	at org.springframework.data.rest.webmvc.mapping.DefaultLinkCollector.createSelfLink(DefaultLinkCollector.java:129)
	at org.springframework.data.rest.webmvc.mapping.DefaultLinkCollector.getLinksFor(DefaultLinkCollector.java:95)
	at org.springframework.data.rest.webmvc.mapping.DefaultLinkCollector.getLinksFor(DefaultLinkCollector.java:79)
	at org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$ProjectionSerializer.toModel(PersistentEntityJackson2Module.java:728)
	at org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$ProjectionSerializer.serialize(PersistentEntityJackson2Module.java:693)
	at org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$ProjectionSerializer.serialize(PersistentEntityJackson2Module.java:650)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:774)
	... 149 common frames omitted

In this case I am using a request to get UserGroups, which has a collection of lazy initialized users.
(much stuff suppressed for easier view)

class UserGroup {
   @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "group_user",
        joinColumns = @JoinColumn(name = "group_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
    private Set<User> users;
}

And the response is intended to have something like this

{
   {
     group: x
     users: [
       {
         name: x
       },
       {
         name: y
       }
     ]
   }
}

The response serialization is failing.

I've been debugging the fix that was added to 2.6.1 and I don't see anywhere going into that method thad was changed.

Can you please advise? We are trying to bump from spring boot 2.5.5 to 2.6.6 due to the recent CVE.

Thank you

Joao
[email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

5 participants