Hibernte configuration and junit test notes, hiberntejunit notes
Hibernate Configuration
Junit Test
Import static org. junit. Assert .*;
Public class HibernateTest {
SessionFactory factory = null;
Session session = null;
// Initialize data before testing
@ Before
Public void setUp () throws Eception {
System. out. println ("initialization ");
Configuration config = new Configuration
(). Configure ();
ServiceRegistry sr = new
ServiceRegistryBuilder. applySetting (config. getProperties ());
Factory = config. buildSessionFactory (sr );
Session = factory. openSession ();
}
@ After
// Release data after testing
Public void tearDown () throws Exception {
System. out. println ("end, release resource ");
If (session. isOpen ()){
Session. close ();
}
}
}
@ Test method
Public void tearDown () throws Exception {
System. out. println ("initialization ");
} @ Test
Public void testAdd (){
Transaction tx = session. beginTransaction ();
Person p = new Person ("admin", 123456, new Date
());
Session. persist (p); // Add a transaction.
Tx. commit ();
}
}
// Hibernate in-depth Configuration
Access
<Hibernate-mapping default-access = "property">
// Field (you do not need the getter/setter method to operate on Object Attributes)
(Try not to set it like this)
Person p = (Person) session. get (Person. class, id); // create Based on reflection
Object
System. out. println (p );
<! --
1. default-access = "field" Object attribute State setting
Property: indicates that this property needs to be bathed using the getter/settter method.
Default usage
2. package: Specify the package name of the class in this persistent configuration file.
Auto-import: Specify the import package
Default-lazy: Specifies the class delay loading policy in this persistent configuration file.
Default-cascade: Specifies the class cascade operation policy in the persistent configuration file.
<Class name = "Person" table = "t_person">
Dynamic-insert: attributes without values are not involved in the add operation.
True: properties without value assignment are not involved in the operation.
Mutable: indicates whether the class can be updated.
False: update is not allowed, but deletion can be added.
Select-before-update: true (an SQL query is initiated before the update is executed,
Check whether the data is inconsistent with the table. If the data is inconsistent, update the data. Otherwise, the data is not updated)
Where = "t_name = test2": A where query condition is attached before the query.
<Property name = "count" formula = "(select (*) from
T_Person) "/>
Query statement corresponding to formula, the data source of an attribute
Public void testAdd (){
Person p = new Person ();
P. setName ("Xxx ");
System. out. println (p );
Session. beginTransaction ();
Session. persist (p );
Tx. commit ();
}
Public void testUpdate (){
Person p = new Person ();
P. setId (1 );
P. setName ("Xxx ");
System. out. println (p );
Session. beginTransaction ();
Session. Update (p); // Id required for Update
Tx. commit ();
}
-->
<! -- Other property ing configurations of property
Name: Specifies the property name to be mapped in the persistence class.
Column: Specifies the column name of the table. If this parameter is left blank, the default attribute name is used)
Length: Specifies the length of the data stored in the column.
Type: Specifies the column field type.
-->
How to Implement Ant Construction Technology in Eclipse SDK and compile Junit test example
Eclipse has already integrated junit to directly create test cases.
Ant is also integrated
For more information, see the eclipse documentation.
Integrating ssh2 has encountered a problem and is easy to answer
When using servlet/jsp, you write a web. xml file. This is special and will automatically reload
However, spring, hibernate, and struts configurations
It won't reload
For SSH development, the test unit should be used for testing!
(I use JUnit4. You can use the test tool that comes with Spring to directly test SSH integration !)
Spring and hibernte also have Struts2.x
They do not depend on the web environment! (Struts2.x Action)
That is to say, you can not deploy them on Tomcat during development or testing !, Instead, use the main or test tool for encoding testing!
This is an SSH development method and part of spiral incremental development!