这是代码
@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() {
}
配置
<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;
}
堆栈跟踪
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)
不知道为什么我收到此错误,我是hibernate的新手,这是我第一个使用hibernate的程序。
搜索各种表单后,我无法解决该错误。
我不确定出了什么问题以及如何解决问题,我怀疑的一点是我错过了一些配置并且欢迎任何提示
由于
答案 0 :(得分:0)
向@Entity
类添加Honey
注释,以便Hibernate将其识别为实体。