Hibernate simple annotation (1. 1), hibernate annotation. 1

Source: Internet
Author: User

Hibernate simple annotation (1. 1), hibernate annotation. 1

**************************************** **************************************** ***************************

Package com. ij34.web;

Import org. hibernate .*;
Import org. hibernate. boot. registry .*;
Import org. hibernate. cfg .*;
Import org. hibernate. service .*;

Import com. ij34.dao. New;

Public class test01 {
Public static void main (String [] args) throws Exception {
// Instantiate Configuration
Configuration conf = new Configuration ()
. AddAnnotatedClass (com. ij34.dao. New. class)
. SetProperty ("hibernate. connection. driver_class", "com. mysql. jdbc. Driver") // you must add hibernate.
. SetProperty ("hibernate. connection. url", "jdbc: mysql: // localhost/hibernate") // you must add hibernate
. SetProperty ("hibernate. connection. username", "root") // you must add hibernate.
. SetProperty ("hibernate. connection. password", "123456") // you must add hibernate
. SetProperty ("hibernate. dialect", "org. hibernate. dialect. MySQL5InnoDBDialect") // hibernate must be added.
. SetProperty ("show_mysql", "true ")
. SetProperty ("hibernate. hbm2ddl. auto", "create"); // you must add hibernate
ServiceRegistry SR = new StandardServiceRegistryBuilder (). applySettings (conf. getProperties (). build ();
// Create a SessionFactory instance with a Configuration instance
SessionFactory SF = conf. buildSessionFactory (SR );
// Create session
Session session = SF. openSession ();
// Start transaction
Transaction tx = session. beginTransaction ();
New n = new New ();
N. setTitle ("hello ");
N. setContent ("2016 hello world 123456789 ");
Session. save (n );
Tx. commit ();
Session. close ();
SF. close ();
}
}

 

 

**************************************** **************************************** *******************

Package com. ij34.dao;
Import javax. persistence .*;
@ Entity
@ Table (name = "test01 ")
Public class New {
@ Id
@ GeneratedValue (strategy = GenerationType. IDENTITY)
Private int id;
Private String title;
Private String content;
Public int getId (){
Return id;
}
Public void setId (int id ){
This. id = id;
}
Public String getTitle (){
Return title;
}
Public void setTitle (String title ){
This. title = title;
}
Public String getContent (){
Return content;
}
Public void setContent (String content ){
This. content = content;
}


}

Related Article

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.