2

I'm using maven multimodule project. I divided my logic into different layers, Presentation, Business logic, data layer, each one in a separate module(layer). When I try to insert an object, this exception occurs:

org.hibernate.MappingException: Unknown entity: com.xxxxx.service.model.Object$Proxy$_$$_WeldClientProxy

How is this caused and how can I solve it?

I'm using CDI bean and the application is based on JSF2 and Hibernate.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
majdi
  • 43
  • 1
  • 9
  • 1
    show your code where you insert object and full exception – z21 Oct 18 '17 at 09:24
  • see https://stackoverflow.com/questions/23214454/org-hibernate-mappingexception-unknown-entity-annotations-users – trunkc Oct 18 '17 at 09:28
  • Welcome to StackOverflow! Please have a look at the [guides for asking questions](https://stackoverflow.com/help/asking), specifically [how to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – AesSedai101 Oct 18 '17 at 09:57

1 Answers1

3

This problem will happen when you have a JPA entity which is also declared as a CDI managed bean like below:

@Named // Or @XxxScoped
@Entity
public class YourEntity {}

And you attempt to persist the CDI managed bean instance itself like below:

@Inject
private YourEntity yourCDIManagedEntity;

@PersistenceContext
private EntityManager entityManager;

public void save() {
    entityManager.persist(yourCDIManagedEntity);
}

This is not the correct way. You should not make your entity a CDI managed bean. A CDI managed bean is actually a proxy class. You can clearly see this back in your exception message. It says it doesn't know the entity com.xxxxx.service.model.Object$Proxy$_$$_WeldClientProxy instead of that it doesn't know the entity com.xxxxx.service.model.Object.

@Entity // NO @Named nor @XxxScoped!
public class YourEntity {}

And you should prepare it as a normal entity instance and then you can safely persist it as a normal entity.

private YourEntity yourNormalEntity;

@PersistenceContext
private EntityManager entityManager;

@PostConstruct
public void init() {
    yourNormalEntity = new YourEntity();
}

public void save() {
    entityManager.persist(yourNormalEntity);
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555