Hibernate annotation of HelloWorld

Source: Internet
Author: User

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
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.