Hibernate使用query.list()得不到正确的记录;

Ghe*_*ham 7 java mysql hibernate

我正在构建一个使用Hibernate和的应用程序Mysql,我的整个数据库都有15表格.

这是问题:我开始将记录插入数据库并使用它query.list();来查看它们以获得添加的记录,在获得"正确"结果一段时间之后,我开始没有我添加的最后一条记录,我再次添加并且它是相同的(之前显示的那个显示但不是我添加的最后一个),我刷新并获得正确的记录,我再次刷新并且我得到32记录而不是43,我没有使用任何复杂的查询只是Select有条件,你可以看到这真的很奇怪.

请注意,我正在保存对象,然后imidiately fetching,所以插入后直接选择(我不知道是否会导致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;
    }
Run Code Online (Sandbox Code Playgroud)

插入产品的代码:

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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是Product实体类:

@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
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

编辑:我试过session.flush()session.clear(),因为我虽然这个问题有兑现的事,但我仍然有同样的问题,我开始认为这是与有问题Mysql Workbench的服务器.

已经有五天了,我简直不敢相信整个stackoverflow社区中没有人对此有任何想法,这和我遇到的问题一样奇怪.

Roc*_*lee 0

我认为随机结果是由于您的 select 语句缺乏事务边界造成的。根据这个休眠文档,你应该

始终使用清晰的事务边界,即使对于只读操作也是如此

尝试更改您的代码并检查结果。

Transaction tx = null;
try {
    tx = session.beginTransaction();
    list = session.createQuery("from Product where deleted= false").list();
    tx.commit();
} catch (Exception e) {
      if (tx != null) tx.rollback();
} finally {
      session.close();
}
Run Code Online (Sandbox Code Playgroud)

详细的讨论可以在这里找到