<project ……> …… <dependencies> …… <dependency> <!-- [Hibernate 3]. --> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.8.Final</version> </dependency> <dependency> <!-- [Hibernate 3] dependence. --> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.11.0.GA</version> </dependency> </dependencies> </project>
<?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>
<!-- Connection parameters: -->
<property
name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property
name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:demo01</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<!-- Database SQL dialect: -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- Console output flags: -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
</session-factory>
</hibernate-configuration>
……
public class _01_Hibernate_Operation
{
private String strClasspathConfFile =
"tfw/integration_demo/_04_spring_web_mvc/_02_crud/_04_hibernate_crud/hibernate-config.xml";
@Test
public void testObjects() throws IOException
{
Configuration conf = new Configuration();
System.out.println("Configuration:\n\t" + conf);
System.out.println("Configuration File:\n\t" + strClasspathConfFile);
conf.configure(strClasspathConfFile);
System.out.println("Configuration:\n\t" + conf);
SessionFactory sf = conf.buildSessionFactory();
System.out.println("SessionFactory:\n\t" + sf);
Session session = sf.openSession();
System.out.println("Session:\n\t" + session);
Transaction ts = session.beginTransaction();
System.out.println("Transaction:\n\t" + ts);
session.close();
System.out.println("Session Closed:\n\t" + session);
}
}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User"
table="tfw_dash_user">
<!-- Special column - Oracle primary key: -->
<id name="id" type="java.lang.Integer" column="id">
<generator class="sequence">
<param name="sequence">tfw_seq_dash_user_id</param>
</generator>
</id>
<!-- Ordinary columns: -->
<property name="name" type="java.lang.String" column="name" />
<property name="passwd" type="java.lang.String" column="passwd" />
<property name="status" type="java.lang.Byte" column="stat" />
<property name="remark" type="java.lang.String" column="remark" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ……> <hibernate-configuration> <session-factory> …… <property name="format_sql">true</property> <!-- Mapping files: --> <mapping resource="tfw/integration_demo/_04_spring_web_mvc/_02_crud/_04_hibernate_crud/dao/impl/oracle/User.hibernate_mapping.xml"/> </session-factory> </hibernate-configuration>
…… public class _01_Hibernate_Operation { private String strClasspathConfFile = "tfw/integration_demo/_04_spring_web_mvc/_02_crud/_04_hibernate_crud/hibernate-config.xml"; private SessionFactory sf; @Test public void testObjects() throws IOException { …… } @Before public void init() throws IOException { Configuration conf = new Configuration(); System.out.println("Configuration:\n\t" + conf); System.out.println("Configuration File:\n\t" + strClasspathConfFile); conf.configure(strClasspathConfFile); System.out.println("Configuration:\n\t" + conf); sf = conf.buildSessionFactory(); System.out.println("SessionFactory:\n\t" + sf); } @Test public void testHibernate_findById() { // [S] Preparing. Integer itgUserId; Session session; { itgUserId = // null; // [!][X] Always exception. new Integer(10000); session = sf.openSession(); System.out.println("Session:\n\t" + session); } // [E] Preparing. // [S] Main. { User user = (User) session.get(User.class, itgUserId); System.out.println("User:\n\t" + user); session.close(); System.out.println("Session Closed:\n\t" + session); } // [E] Main. } }
…… public class _01_Hibernate_Operation { …… @Test public void testHibernate_findById() { …… } @Test public void testHibernate_listByName() { String strHql; Session session; { String strUserName = // null; // "Name_AAA"; "root"; strHql = "from User where name=" + (null == strUserName ? null : "'" + strUserName + "'") + " order by name, id"; System.out.println("HQL:\n\t" + strHql); session = sf.openSession(); System.out.println("Session:\n\t" + session); } // [E] Preparing. // [S] Main. { Query query = session.createQuery(strHql); System.out.println("Query:\n\t" + query); // [!] User with null ID listed as null row. @SuppressWarnings("unchecked") List<User> lstUsers = query.list(); session.close(); System.out.println("Session Closed:\n\t" + session); showUsers(lstUsers); } // [E] Main. } @Test public void testHibernate_listByName2() { // [S] Preparing. String strUserName; String strHql; Session session; { strUserName = // null; // [!] Miss-spelled SQL, no effect. // "Name_AAA"; "root"; strHql = "from User where name=:name order by name, id"; System.out.println("HQL:\n\t" + strHql); session = sf.openSession(); System.out.println("Session:\n\t" + session); } // [E] Preparing. // [S] Main. { Query query = session.createQuery(strHql); System.out.println("Query:\n\t" + query); query = query.setString("name", strUserName); System.out.println("Query:\n\t" + query); // [!] User with null ID listed as null row. @SuppressWarnings("unchecked") List<User> lstUsers = query.list(); session.close(); System.out.println("Session Closed:\n\t" + session); showUsers(lstUsers); } // [E] Main. } private void showUsers(List<User> lstUsers) { …… } }
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE ……> <hibernate-mapping> <class ……> …… <!-- Mappings of ordinary columns: --> <property name="name" type="java.lang.String" column="name" /> <property name="passwd" type="java.lang.String" column="passwd" /> <property name="status" type="java.lang.Byte" column="stat" /> <property name="remark" type="java.lang.String" column="remark" /> <!-- Customized queries: --> <query name="listByName"> from User where name=:name order by name, id </query> <query name="listAll"> from User order by name, id </query> </class> </hibernate-mapping>
…… public class _01_Hibernate_Operation { …… @Test public void testHibernate_listByName2() { …… } @Test public void testHibernate_listByName3() { // [S] Preparing. String strUserName; Session session; { strUserName = // null; // [!] Miss-spelled SQL, no effect. // "Name_AAA"; "root"; session = sf.openSession(); System.out.println("Session:\n\t" + session); } // [E] Preparing. // [S] Main. { Query query = session.getNamedQuery( "tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User.listByName"); System.out.println("Query:\n\t" + query); query = query.setString("name", strUserName); System.out.println("Query:\n\t" + query); // [!] User with null ID listed as null row. @SuppressWarnings("unchecked") List<User> lstUsers = query.list(); session.close(); System.out.println("Session Closed:\n\t" + session); showUsers(lstUsers); } // [E] Main. } @Test public void testHibernate_listAll() { // Preparing. Session session = sf.openSession(); System.out.println("Session:\n\t" + session); // [S] Main. { Query query = session.getNamedQuery( "tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User.listAll"); System.out.println("Query:\n\t"); // [!] User with null ID listed as null row. @SuppressWarnings("unchecked") List<User> lstUsers = query.list(); session.close(); System.out.println("Session Closed:\n\t" + session); showUsers(lstUsers); } // [E] Main. } private void showUsers(List<User> lstUsers) { …… } }