Hibernate:没有为实体指定标识符

时间:2016-04-17 02:46:36

标签: java mysql hibernate

我开发了一个简单的Web应用程序,它通过hibernate连接到MySQL。在某种程度上,我甚至成功地创建了一个连接并在数据库中插入了java对象。然而,我之后做了一些改变以满足我的项目需求,在那里我搞砸了。我收到此错误“没有为实体指定标识符”无论我多少尝试调试它。我甚至从零开始创建了一个新项目但是徒劳无功。请帮我识别我做错了什么。

   package com.proj.beandb;

   import javax.faces.bean.ManagedBean;
   import javax.faces.bean.SessionScoped;
   import javax.persistence.Column;
   import javax.persistence.Entity;
   import javax.persistence.Id;
   import javax.persistence.Table;

   import org.hibernate.Session;
   import org.hibernate.SessionFactory;
   import org.hibernate.cfg.Configuration;

   @ManagedBean
   @SessionScoped
   @Entity
   @Table(name="dbstat")
   public class Hibernate 
  {
  @Id
  @Column(name="first_name")
  private static String fname;

@Column(name="last_name")
private static String lname;

  public Hibernate(){}

   public Hibernate(String fname, String lname) 
      {
      Hibernate.fname = fname;
      Hibernate.lname = lname;
      }

        public static void main(String[] args) {

             // create session factory
             SessionFactory factory = new Configuration().configure("hibernate.cfg.xml")
                               .addAnnotatedClass(Student.class)
                               .buildSessionFactory();

             // create session
             Session session = factory.getCurrentSession();

             try {       
                // create a student object
                System.out.println("Creating new object");
                Hibernate tempRec = new Hibernate("bah", "blah");

                // start a transaction
                session.beginTransaction();

                // save the object
                session.save(tempRec);

                // commit transaction
                session.getTransaction().commit();

                System.out.println("Done!");
             }
             finally {
                factory.close();
             }
          }

           public String getFname() {
                  return fname;
               }

               public void setFname(String fname) {
                  Hibernate.fname = fname;
               }

               public String getLname() {
                  return lname;
               }

               public void setLname(String lname) {
                  Hibernate.lname = lname;
               }
      }

`

1 个答案:

答案 0 :(得分:1)

@Id @Column(name="first_name") private static String fname; @Column(name="last_name") private static String lname; 字段用作持久化类的属性是一个非常大的错误。

@Id
@Column(name="first_name")
private String fname;

@Column(name="last_name")
private String lname;

应该是

@ManagedBean
@SessionScoped

你不需要它,因为它不是Hibernate相关的东西

Student

您使用addAnnotatedClass(Student.class)添加Hibernate课程,也许您需要添加main()课程。

请不要将Hibernate放在持久课堂中。您需要拥有HibernateTest课程,例如main()课程<div id="inputbox" style="border:1px solid lightgrey" contenteditable="true"> <span id="1">word1</span> <span id="2">word2</span> <span id="3">word3</span> <span id="4">word4</span> <span id="5">word5</span> <span id="6">word6</span> <span id="7">word7</span> <span id="8">word8</span> <span id="9">word9</span> <span id="10">word10</span> <span id="11">word11</span> <span id="12">word12</span> </div>