我正在使用h2和hibernate构建一个带有spring boot的spring boot应用程序。我正在尝试一个查询,但没有得到任何结果,但当我做同样的事情mysql我得到一个结果。没有错误,当我查询我的h2数据库时,我得到一个空的结果。任何想法都是为什么我可以从一个而不是另一个产生结果?
h2 config
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:h2:mem:todo;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</property>
<property name="connection.username">sa</property>
<property name="connection.password" />
<!--Set the database dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<!--Echo all executed SQL to stdout-->
<property name="show_sql">true</property>
<!--Drop and re-create the database schema on startup-->
<property name="hbm2ddl.auto">create</property>
<property name="connection.driver_class">org.h2.Driver</property>
<!--Name the annotated Entity classes -->
<mapping class="com.todo.beans.User" />
</session-factory>
</hibernate-configuration>
MySQL配置
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<!--Database connection settings -->
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/todo</property>
<property name="connection.username">root</property>
<property name="connection.password">password</property>
<!--Set the database dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!--Echo all executed SQL to stdout-->
<property name="show_sql">true</property>
<!--Drop and re-create the database schema on startup-->
<property name="hbm2ddl.auto">update</property>
<!--Name the annotated Entity classes -->
<mapping class="com.todo.beans.User" />
</session-factory>
</hibernate-configuration>
来自DAO的查询
public List<User> getAllUsers() {
Session session = sessionFactory.openSession();
session.beginTransaction();
Query query = session.createQuery("from User");
return (List<User>) query.getResultList();
}
用户类
@Entity
@Table(name="USERS")
public class User implements Serializable {
@Id @GeneratedValue
long id;
String email;
String password;
Date createdAt;
Date updatedAt;
public User() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
}
两张牌都以相同的方式设置
CREATE TABLE users(id int auto_increment primary key, email varchar(50), password varchar(50), createdat date, updatedat date);