I\'m finding my feet with Hibernate Annotations and I\'ve hit a problem I hope someone can help with.
I have 2 entities, Section and ScopeTopic. The section has a Li
In my case a has to add my classes, when building the SessionFactory
, with addAnnotationClass
Configuration configuration.configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory sessionFactory = configuration
.addAnnotatedClass(MyEntity1.class)
.addAnnotatedClass(MyEntity2.class)
.buildSessionFactory(builder.build());
Your entity may not listed in hibernate configuration file.
Your annotations look fine. Here are the things to check:
make sure the annotation is javax.persistence.Entity
, and not org.hibernate.annotations.Entity
. The former makes the entity detectable. The latter is just an addition.
if you are manually listing your entities (in persistence.xml, in hibernate.cfg.xml, or when configuring your session factory), then make sure you have also listed the ScopeTopic
entity
make sure you don't have multiple ScopeTopic
classes in different packages, and you've imported the wrong one.
Mine was not having @Entity
on the many side entity
@Entity // this was commented
@Table(name = "some_table")
public class ChildEntity {
@JoinColumn(name = "parent", referencedColumnName = "id")
@ManyToOne
private ParentEntity parentEntity;
}
Mostly in Hibernate
, need to add the Entity
class in hibernate.cfg.xml
like-
<hibernate-configuration>
<session-factory>
....
<mapping class="xxx.xxx.yourEntityName"/>
</session-factory>
</hibernate-configuration>
If you are using Java configuration in a spring-data-jpa project, make sure you are scanning the package that the entity is in. For example, if the entity lived com.foo.myservice.things then the following configuration annotation below would not pick it up.
You could fix it by loosening it up to just com.foo.myservice (of course, keep in mind any other effects of broadening your scope to scan for entities).
@Configuration
@EnableJpaAuditing
@EnableJpaRepositories("com.foo.myservice.repositories")
public class RepositoryConfiguration {
}