HelloWorld HelloWorld - 2 months ago 19
Java Question

@OneToOne or @ManyToOne on com.hibernate.domain.Bee.honey references an unknown entity

here is code

@SequenceGenerator(name = "honey_seq", sequenceName = "honey_id_seq")
public class Honey implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "honey_seq")
private Integer id;
private String name;
private String taste;
@OneToMany(mappedBy = "honey")
private Set<Bee> bees = new HashSet<>();



@Entity
public class Bee implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "bee_gen")
@SequenceGenerator(name = "bee_gen", sequenceName = "bee_id_seq")
private Integer id;
private String name;

@ManyToOne
@JoinColumn
private Honey honey;

public Bee() {
}


Configuration

<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/sweet</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>


<!-- Mappings -->

<mapping class="com.hibernate.domain.Honey" />
<mapping class="com.hibernate.domain.Bee" />
</session-factory>


Client.java

public static Honey createHoney() {
Honey forestHoney = new Honey();
forestHoney.setName("forest Honey");
forestHoney.setTaste("very sweet");
Session sessoin = (Session) InitSessionFactory.getSessionFactoryInstance();
Transaction transaction = sessoin.beginTransaction();
sessoin.save(forestHoney);
transaction.commit();
sessoin.close();
return forestHoney;
}


Stacktrace

Exception in thread "main" org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.hibernate.domain.Bee.honey references an unknown entity: com.hibernate.domain.Honey
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:97)


Not sure why I am getting this error, I am new to hibernate and this is my first program using hibernate.

After Searching various forms I'm not able to solve that error.

I'm not sure what went wrong and how to fix it,The one thing I doubt is that I'm missing some configuration and any hint is welcomed

Thanks

Answer

Add an @Entity annotation to your Honey class so that Hibernate recognizes it as an entity.

Comments