gpt4 book ai didi

java - Hibernate创建表但不设置对象

转载 作者:行者123 更新时间:2023-11-29 18:46:18 26 4
gpt4 key购买 nike

我的新项目遇到一些问题。我尝试使用创建对象的简单类来检查此设置的正确性。启动 Hibernate 后,创建一个包含所有注释类参数的表。但没有输入有关我设置的对象的信息。

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 + "]";
}
}

我的简单类(class):

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

}

当我开始简单的类(class)时MySQL记录:

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

最佳答案

如果你想持久化对象,你必须启动事务。

尝试:

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

关于java - Hibernate创建表但不设置对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44612728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com