Hibernate 4.3.7 Programmable mode + annotations

Source: Internet
Author: User
Tags jboss

1. Copy the jar file to Lib

Antlr-2.7.7.jar
Dbmysql.jar
Dboracle.jar
Dbsqljdbc2005.jar
Dom4j-1.6.1.jar
Hibernate-commons-annotations-4.0.5.final.jar
Hibernate-core-4.3.7.final.jar
Hibernate-jpa-2.1-api-1.0.0.final.jar
Jandex-1.1.0.final.jar
Javassist-3.18.1-ga.jar
Jboss-logging-3.1.3.ga.jar
Jboss-logging-annotations-1.2.0.beta1.jar
Jboss-transaction-api_1.2_spec-1.0.0.final.jar

2. Annotated Student class

 Packagecom.entity;ImportJavax.persistence.Column;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;Importjavax.persistence.Id; @Entity Public classStudent {Private intID; PrivateString name; @Id @GeneratedValue Public intGetId () {returnID;}  Public voidSetId (intID) { This. ID =ID;} @Column (Name= "Name", length = 20)     PublicString GetName () {returnname;}  Public voidSetName (String name) { This. Name =name;}}

3.hibernateutil.java Programmable Mode class

 Packagecom;Importjava.util.List;Importjava.util.Properties;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.Transaction;ImportOrg.hibernate.boot.registry.StandardServiceRegistryBuilder;Importorg.hibernate.cfg.Configuration;ImportOrg.hibernate.service.ServiceRegistry;Importcom.entity.Student; Public classHibernateutil { Public Static voidMain (string[] args) {Properties m=NewProperties (); M.put ("Hibernate.dialect", "Org.hibernate.dialect.MySQL5Dialect"); M.put ("Hibernate.connection.driver_class", "Com.mysql.jdbc.Driver"); M.put ("Hibernate.connection.url", "Jdbc:mysql://localhost:3306/db?useunicode=true&characterencoding=utf8"); M.put ("Hibernate.connection.username", "root"); M.put ("Hibernate.connection.password", "Fengze"); M.put ("Hibernate.hbm2ddl.auto", "Update"); Properties o=NewProperties (); O.put ("Hibernate.dialect", "Org.hibernate.dialect.Oracle10gDialect"); O.put ("Hibernate.connection.driver_class", "Oracle.jdbc.OracleDriver"); O.put ("Hibernate.connection.url", "Jdbc:oracle:thin: @localhost: 1521:orcl"); O.put ("Hibernate.connection.username", "system"); O.put ("Hibernate.connection.password", "FengZe2012"); O.put ("Hibernate.hbm2ddl.auto", "Update"); Properties s=NewProperties (); S.put ("Hibernate.dialect", "Org.hibernate.dialect.SQLServer2005Dialect"); S.put ("Hibernate.connection.driver_class", "Com.microsoft.sqlserver.jdbc.SQLServerDriver"); S.put ("Hibernate.connection.url", "jdbc:sqlserver://localhost:1433;databasename=db"); S.put ("Hibernate.connection.username", "sa"); S.put ("Hibernate.connection.password", "Fengze"); S.put ("Hibernate.hbm2ddl.auto", "Update"); //Configuration cfg = new configuration (). setproperties (m);Configuration cfg =NewConfiguration (). SetProperties (s); Cfg.addannotatedclass (com.entity.Student.class); Serviceregistry Serviceregistry=NewStandardserviceregistrybuilder (). Applysettings (Cfg.getproperties ()). build (); Sessionfactory SF=cfg.buildsessionfactory (serviceregistry); Session Session=sf.opensession (); //Inserting DataTransaction tx =session.begintransaction (); Student S1=NewStudent (); S1.setname ("Zhang San Feng");        Session.save (S1);                Tx.commit (); List<Student> stu = Session.createquery ("From Student"). List ();  for(Student st:stu) {System.out.println (St.getname ());          } session.close ();      Sf.close (); }}

Hibernate 4.3.7 Programmable mode + annotations

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.