0

Here my target entity which is User.class is present in a separate jar file.

package com.aa.model;
    import javax.persistence.Entity;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToOne;

@Entity
@Table(name = "ABC")
public class Abc implements Serializable{
---    
@OneToOne(targetEntity=com.bb.model.User.class)
    @JoinColumn(name  = "CREATED_BY")
    private User createdBy;
}
---
}

The target class:

package com.bb.model;
    import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="USER")
public class User implements Serializable {
---
}

I am getting the following error:

Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.aa.model.Abc.createdBy references an unknown entity: com.bb.model.User
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:109)
    at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1598)
Aadil
  • 45
  • 3
  • 11
  • This looks pretty similar: https://stackoverflow.com/questions/3983135/hibernate-manytoone-references-an-unknown-entity – sofarsoghood Feb 04 '18 at 08:40

1 Answers1

0

In the class Abc, you wrote

@OneToOne(targetEntity=com.bb.User.class)

instead of

@OneToOne(targetEntity=com.bb.model.User.class)

You forgot also the import of com.bb.model package

import com.bb.model.User;
debe
  • 1,752
  • 2
  • 12
  • 11
  • it is correct only.. i wrote it wrong while editing. – Aadil Feb 03 '18 at 14:00
  • Looks like an entity mapping error to me. Maybe this helps: https://www.thoughts-on-java.org/mapping-definitions-jpa-hibernate-annotations-xml/ – sofarsoghood Feb 03 '18 at 14:13
  • No issue with the mapping as the code works fine if I move the class to the current micro service and use it instead of the one in separate jar file. – Aadil Feb 03 '18 at 14:47
  • So you are using multiple jars file, right? Can you try that solution: it seem the same error you got. https://stackoverflow.com/questions/39546120/use-hibernate-entities-from-external-jar – debe Feb 03 '18 at 15:07
  • I tried it as: `entitymanager.packagesToScan:com.currrentproject.*,com.externaljar.*` as well as `entitymanager.packagesToScan: com.*` however it failed both ways – Aadil Feb 03 '18 at 16:57
  • what is the proper way to configure two different packages to scan in application.properties? – Aadil Feb 03 '18 at 18:57