Hibernate uses the Java standard JPA annotation. This example uses the SQLServer 2005 database.
Project Structure:
Hibernate. cfg. xml
[Html]
<? 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>
<Property name = "hibernate. connection. driver_class"> com. microsoft. sqlserver. jdbc. SQLServerDriver </property>
<Property name = "hibernate. connection. url"> jdbc: sqlserver: // 192.168.1.101: 1433; databaseName = Bank </property>
<Property name = "hibernate. connection. username"> sa </property>
<Property name = "hibernate. connection. password"> xxxxxx </property>
<Property name = "hibernate. dialect"> org. hibernate. dialect. SQLServerDialect </property>
<Property name = "connection. pool_size"> 1 </property>
<Property name = "show_ SQL"> true </property>
<Property name = "hbm2ddl. auto"> create </property>
</Session-factory>
</Hibernate-configuration>
<? 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>
<Property name = "hibernate. connection. driver_class"> com. microsoft. sqlserver. jdbc. SQLServerDriver </property>
<Property name = "hibernate. connection. url"> jdbc: sqlserver: // 192.168.1.101: 1433; databaseName = Bank </property>
<Property name = "hibernate. connection. username"> sa </property>
<Property name = "hibernate. connection. password"> xxxxxx </property>
<Property name = "hibernate. dialect"> org. hibernate. dialect. SQLServerDialect </property>
<Property name = "connection. pool_size"> 1 </property>
<Property name = "show_ SQL"> true </property>
<Property name = "hbm2ddl. auto"> create </property>
</Session-factory>
</Hibernate-configuration>
Account. java
[Java]
Package com. cdai. orm. hibernate. annotation;
Import java. io. Serializable;
Import javax. persistence. Column;
Import javax. persistence. Entity;
Import javax. persistence. Id;
Import javax. persistence. Table;
@ Entity
@ Table (name = "tb_account ")
Public class Account implements Serializable {
Private static final long serialVersionUID = 5018821760412231859L;
@ Id
@ Column (name = "col_id ")
Private long id;
@ Column (name = "col_balance ")
Private long balance;
Public Account (long id, long balance ){
This. id = id;
This. balance = balance;
}
Public long getId (){
Return id;
}
Public void setId (long id ){
This. id = id;
}
Public long getBalance (){
Return balance;
}
Public void setBalance (long balance ){
This. balance = balance;
}
@ Override
Public String toString (){
Return "Account [id =" + id + ", balance =" + balance + "]";
}
}
Package com. cdai. orm. hibernate. annotation;
Import java. io. Serializable;
Import javax. persistence. Column;
Import javax. persistence. Entity;
Import javax. persistence. Id;
Import javax. persistence. Table;
@ Entity
@ Table (name = "tb_account ")
Public class Account implements Serializable {
Private static final long serialVersionUID = 5018821760412231859L;
@ Id
@ Column (name = "col_id ")
Private long id;
@ Column (name = "col_balance ")
Private long balance;
Public Account (long id, long balance ){
This. id = id;
This. balance = balance;
}
Public long getId (){
Return id;
}
Public void setId (long id ){
This. id = id;
}
Public long getBalance (){
Return balance;
}
Public void setBalance (long balance ){
This. balance = balance;
}
@ Override
Public String toString (){
Return "Account [id =" + id + ", balance =" + balance + "]";
}
}
Main Method
[Java]
Package com. cdai. orm. hibernate. annotation;
Import org. hibernate. Session;
Import org. hibernate. SessionFactory;
Import org. hibernate. Transaction;
Import org. hibernate. cfg. AnnotationConfiguration;
Public class AnnotationHibernate {
Public static void main (String [] args ){
SessionFactory sessionFactory = new AnnotationConfiguration ().
AddFile ("hibernate/hibernate. cfg. xml ").
Configure ().
AddPackage ("com. cdai. orm. hibernate. annotation ").
AddAnnotatedClass (Account. class ).
BuildSessionFactory ();
Session session = sessionFactory. openSession ();
Transaction tx = null;
Try {
Tx = session. beginTransaction ();
Session. save (new Account (1,100 ));
Tx. commit ();
}
Catch (Exception e ){
If (tx! = Null)
Tx. rollback ();
}
Finally {
Session. close ();
}
}
}
Package com. cdai. orm. hibernate. annotation;
Import org. hibernate. Session;
Import org. hibernate. SessionFactory;
Import org. hibernate. Transaction;
Import org. hibernate. cfg. AnnotationConfiguration;
Public class AnnotationHibernate {
Public static void main (String [] args ){
SessionFactory sessionFactory = new AnnotationConfiguration ().
AddFile ("hibernate/hibernate. cfg. xml ").
Configure ().
AddPackage ("com. cdai. orm. hibernate. annotation ").
AddAnnotatedClass (Account. class ).
BuildSessionFactory ();
Session session = sessionFactory. openSession ();
Transaction tx = null;
Try {
Tx = session. beginTransaction ();
Session. save (new Account (1,100 ));
Tx. commit ();
}
Catch (Exception e ){
If (tx! = Null)
Tx. rollback ();
}
Finally {
Session. close ();
}
}
}
Run the query in the remote SQL Server database instance. You can see that the data has been inserted into the table!
Author: dc_726