我正在构建一个使用 Hibernate
和 Mysql
的应用程序,我的整个数据库有 15
表。
问题是:我开始将记录插入数据库并使用 query.list();
查看它们以获取添加的记录,但是在获得“正确”结果一段时间后,我开始没有我添加的最后一条记录,我再次添加并且它是相同的(之前显示的记录但不是我添加的最后一条),我刷新并获得正确的记录,我再次刷新并得到 32
记录而不是 43
,我没有使用任何复杂的查询,只是一个带有条件的 Select
,正如你所看到的,这真的很奇怪。
请注意,我正在保存对象然后立即获取,因此直接插入后跟选择(我不知道这是否会导致 Mysql 出现问题),记录也完美地添加到数据库中,使用Mysql workbench
我可以看到我的记录已正确添加到数据库中,我真的希望至少有人能帮助我调试它,因为我没有收到任何错误。
我正在使用 Hibernate 4.3.10
和 java 版本 1.8.0_131
这是一段代码,当从我使用的实体之一获取结果时,“有时”会给我带来问题:
获取产品列表:
public static List<Product> productsList() {
//singleton factory object
SessionsGenerator FactoryObject = new SessionsGenerator();
Session session = SessionsGenerator.getFactory().openSession();
List<Product> list = new ArrayList<>();
try {
list = session.createQuery("from Product where deleted= false").list();
System.out.println("-------------- list product: "+list); // testing from console here I get the same results on the user interface, so it can't be a Javafx problem.
} finally {
session.close();
}
return list;
}
插入产品的代码:
public static boolean SaveOrUpdate(Product product) {
SessionsGenerator FactoryObject = new SessionsGenerator();
Session session = SessionsGenerator.getFactory().openSession();
try {
session.beginTransaction();
session.saveOrUpdate(product);
session.getTransaction().commit();
} catch (Exception e) {
return false;
} finally {
session.close();
return true;
}
}
这是产品实体类:
@Entity
@Table(name = "Product")
public class Product{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false)
int id;
@Column(name = "code", nullable = false)
String code;
@Column(name = "matricule", nullable = false)
String matricule;
@Column(name = "marque", nullable = false)
String marque;
@Column(name = "type", nullable = false)
String type;
@OneToMany(targetEntity = Facture.class, mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Facture> factures;
@OneToMany(targetEntity = Achat.class, mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Achat> achats;
@Column(name = "deleted", nullable = false)
boolean deleted;
public Product() {
}
public Product(String code, String matricule, String marque,String type) {
this.code = code;
this.matricule = matricule;
this.marque = marque;
this.type = type;
this.deleted = false;
}
//setters and getters
这是我的 hibernate 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/gestioncommerciale</property>
<property name="connection_pool_size">1</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
编辑:我尝试了 session.flush()
和 session.clear()
,因为我虽然问题与兑现有关,但我仍然有同样的问题问题,我开始认为这是 Mysql Workbench
服务器的问题。
已经五天了,我无法相信整个 stackoverflow 社区中没有人对此有丝毫的想法,这和我遇到的问题一样奇怪。
最佳答案
发生这种情况绝对很奇怪。乍一看,代码似乎很好,而且我没有发现您获取数据的方式有任何问题。
您提到您在开始时得到了正确的响应,但过了一段时间后返回了不正确的数据。
我认为这是因为您每次都在创建一个新 session ,而实际上并没有在完成后关闭它。
基本上,改变这个:
SessionsGenerator.getFactory().openSession();
收件人:
SessionsGenerator.getFactory().getCurrentSession();
当使用 openSession() 时,您需要显式刷新并关闭对象。这可能是您遇到问题的原因。
如果 session 不存在,getCurrentSession() 会打开一个新 session ,并在完成后自动刷新并关闭 session 。
此外,将其添加到您的 hibernate.cfg.xml 中
<session-factory>
<!-- Put other elements here -->
<property name="hibernate.current_session_context_class">
thread
</property>
</session-factory>
否则,您将遇到异常,因为您没有正确配置使用 getCurrentSession。
此外,尝试调用
session.flush();
session.close();
相反,如果您不想采用上述方法。
希望这能解决问题!
关于java - Hibernate 没有使用 query.list() 获得正确的记录;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45153020/