Hibrenate创建一个表但不设置对象

时间:2017-06-18 07:35:24

标签: java mysql hibernate

我的新项目遇到了一些问题。我尝试检查正确的工作这个设置与我创建对象的简单类。启动后,Hibernate创建一个包含所有anotate类paramentres的表。但没有把我设置的对象的信息。

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>

对象类:

@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);
}

}

我的道:

@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

1 个答案:

答案 0 :(得分: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);
}