<hibernate-mapping>
<class name="User" table="users">
<meta attribute="class-description">
This class contains the user detail.
</meta>
<id name="id" type="string" column="id">
</id>
<property name="displayName" column="displayName" type="string"/>
<property name="status" column="status" type="string"/>
<property name="kind" column="kind" type="string"/>
</class>
</hibernate-mapping>
并配置如下
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 顯示實際操作資料庫時的SQL -->
<property name="show_sql">true</property>
<!-- 將顯示的SQL排版,方便觀看 -->
<property name="format_sql">true</property>
<!-- SQL方言,這邊設定的是MySQL -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- JDBC驅動程式 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- JDBC URL -->
<property name="connection.url">jdbc:mysql://localhost/comp4111-database?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
</property>
<!-- 資料庫使用者 -->
<property name="connection.username">samwalker</property>
<!-- 資料庫密碼 -->
<property name="connection.password">*sam505*</property>
<property name="hbm2ddl.auto">update</property>
<!-- 物件與資料庫表格映射文件 -->
<mapping resource="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
当我尝试从session.load获取单个用户时,它很好。但是当我使用以下代码时:
public void getUsers() {
Session session = factory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
List users = session.createQuery("from User").list();
users.forEach(user -> print(((User)user).getId()));
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
}
它似乎显示/ u0000(Idea控制台中的空白区域),我不知道为什么,任何人都可以帮助我吗?非常感谢