2017-06-18 3 views
0

を設定しません。私は正しい仕事に私は私のオブジェクトを作成する単純なクラスwhithこの設定をチェックしてみてください。スタート後はHibernateは、すべてのanotateクラスparamentres whithテーブルを作成します。しかし、私が設定したオブジェクトについての情報は入れていませんでした。Hibrenateは、テーブルを作成するが、私は私の新しいプロジェクトのいくつかの問題を持っているオブジェクト

hibernate.cgf.xml

<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
<session-factory> 
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="connection.url">jdbc:mysql://localhost/DiplomBD?serverTimezone=UTC</property> 
    <property name="connection.username">root</property> 
    <property name="connection.password">admin12345</property> 
    <property name="connection.pool_size">10</property> 
    <!-- <property name="hibernate.connection.autocommit">false</property> --> 
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="show_sql">true</property> 
    <property name="hbm2ddl.auto">update</property> 
    <property name="current_session_context_class">thread</property> 
</session-factory> 

Objectクラス:

@Entity 
@Table(name = "Tests") 
public class Test { 
    @Id 
    @Column(name = "id", updatable = false, nullable = false) 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Integer id; 
    @Column(name = "nameTest") 
    private String name; 
    @Column(name = "description") 
    private String description; 
    @Column(name = "isFree") 
    private boolean isFree; 
    @Column(name = "status") 
    private int status; 
    @Column(name = "autor") 
    private String autor; 
    @Column(name = "section") 
    private String section; 
    @Column(name = "commentToAdmin") 
    private String commentToAdmin; 

    public Test() { 
    } 

    public String getAutor() { 
     return autor; 
    } 

    public void setAutor(String autor) { 
     this.autor = autor; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

    public boolean isFree() { 
     return isFree; 
    } 

    public void setFree(boolean isFree) { 
     this.isFree = isFree; 
    } 

    public int getStatus() { 
     return status; 
    } 

    public void setStatus(int status) { 
     this.status = status; 
    } 

    public String getCommentToAdmin() { 
     return commentToAdmin; 
    } 

    public void setCommentToAdmin(String commentToAdmin) { 
     this.commentToAdmin = commentToAdmin; 
    } 

    public String getSection() { 
     return section; 
    } 

    public void setSection(String section) { 
     this.section = section; 
    } 

    @Override 
    public String toString() { 
     return "Test [id=" + id + ", name=" + name + ", description=" + description + ", isFree=" + isFree + ", status=" 
      + status + ", autor=" + autor + ", section=" + section + ", commentToAdmin=" + commentToAdmin + "]"; 
    } 
} 

私の単純なクラス:

public class Test1Hibernate { 
public static void main(String[] args) { 
    Test test = new Test(); 

    test.setId(1); 
    test.setName("Experiment"); 
    test.setDescription("Some test"); 
    test.setFree(true); 
    test.setSection("IT"); 
    test.setStatus(1); 
    test.setAutor("Me"); 
    TestDAOInterface testDao = new TestDAOImpl(); 
    testDao.addPerson(test); 
} 

}

マイDAO:

@Repository 
public class TestDAOImpl implements TestDAOInterface { 

private static final Logger logger = LoggerFactory.getLogger(TestDAOInterface.class); 

private SessionFactory sessionFactory; 

public void setSessionFactory(SessionFactory sf) { 
    this.sessionFactory = sf; 
} 

@Override 
public void addPerson(Test p) { 
    // TODO Auto-generated method stub 
    Session session = this.sessionFactory.getCurrentSession(); 
    session.persist(p); 
    logger.info("Test saved successfully, Test Details=" + p); 
} 

@Override 
public void updateTest(Test p) { 
    // TODO Auto-generated method stub 
    Session session = this.sessionFactory.getCurrentSession(); 
    session.update(p); 
    logger.info("Test updated successfully, Test Details=" + p); 
} 

@Override 
public List<Test> listTests() { 
    // TODO Auto-generated method stub 
    Session session = this.sessionFactory.getCurrentSession(); 
    List<Test> personsList = session.createQuery("from Person").list(); 
    for (Test p : personsList) { 
     logger.info("Person List::" + p); 
    } 
    return personsList; 
} 

@Override 
public Test getTestById(int id) { 
    // TODO Auto-generated method stub 
    Session session = this.sessionFactory.getCurrentSession(); 
    Test p = (Test) session.load(Test.class, new Integer(id)); 
    logger.info("Test loaded successfully, Test details=" + p); 
    return p; 
} 

@Override 
public void removeTest(int id) { 
    // TODO Auto-generated method stub 
    Session session = this.sessionFactory.getCurrentSession(); 
    Test p = (Test) session.load(Test.class, new Integer(id)); 
    if (null != p) { 
     session.delete(p); 
    } 
    logger.info("Test deleted successfully, Test details=" + p); 
} 

}

MySQLのログは、私は単純なクラスを開始します。あなたがオブジェクトを永続化する場合

10:21:42 SELECT * FROM diplombd.tests LIMIT 0, 1000 0 row(s) returned 0.000 sec/0.000 sec 
+0

使用@Transactional –

+0

'TestDAOInterface testDao =新しいTestDAOImpl();'のSpring Beanは、この方法でインスタンス化することはできません。豆をロードし、コンテキストから、あなたのDAO Beanを取得するために 'ApplicationContext'を使用してください。 – davidxxx

+0

私は「@Transactional」を追加を試してみました、それは(= – BruceVVayne

答えて

0

、あなたが取引を開始する必要があります。

試してみてください。

@Override 
public void addPerson(Test p) { 
    // TODO Auto-generated method stub 
    Session session = this.sessionFactory.getCurrentSession(); 
    session.beginTransaction(); 
    session.persist(p); 
    session.getTransaction().commit(); 
    logger.info("Test saved successfully, Test Details=" + p); 
} 
+0

に助けされていません(( – BruceVVayne

+0

は同上のでSETID機能を使用していない=助けにはなりませんでした。 –

+0

オーケーを挿入しながら、自動的に私はそれをremuwed決定されAaaaaaand何も変更を:( – BruceVVayne

関連する問題