Maven
<!--importing org.hibernate jar packages - <Dependency> <groupId>Org.hibernate</groupId> <Artifactid>Hibernate-core</Artifactid> <version>4.1.4.Final</version> </Dependency> <!--import MySQL Database link jar package - <Dependency> <groupId>Mysql</groupId> <Artifactid>Mysql-connector-java</Artifactid> <version>5.1.6</version> </Dependency>
Hibernate.cfg.xml configuration file
<?XML version= ' 1.0 ' encoding= ' UTF-8 '?><!DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http://w Ww.hibernate.org/dtd/hibernate-configuration-3.0.dtd "><hibernate-configuration> <session-factory> < Propertyname= "dialect">Org.hibernate.dialect.MySQLDialect</ Property> < Propertyname= "Connection.url">Jdbc:mysql://localhost:3306/test2</ Property> < Propertyname= "Connection.username">Root</ Property> < Propertyname= "Connection.password">sa1234$</ Property> < Propertyname= "Connection.driver_class">Com.mysql.jdbc.Driver</ Property> < Propertyname= "Myeclipse.connection.profile">Mysql</ Property> <!--Echo all executed SQL to stdout - < Propertyname= "Show_sql">True</ Property> < Propertyname= "Format_sql">True</ Property> <MappingResource= "User.hbm.xml" /> </session-factory></hibernate-configuration>
Entity class
Packagemy; Public classUserImplementsjava.io.Serializable {// Fields PrivateLong userId; PrivateString UserName; PrivateString password; PrivateString Comment; //Constructors PublicUser () {}//Property Accessors PublicLong getUserId () {return This. userId; } Public voidSetuserid (Long userId) { This. UserId =userId; } PublicString GetUserName () {return This. UserName; } Public voidsetusername (String userName) { This. UserName =UserName; } PublicString GetPassword () {return This. Password; } Public voidSetPassword (String password) { This. Password =password; } PublicString getcomment () {return This. Comment; } Public voidsetcomment (String comment) { This. Comment =comment; }}
mapping files
<?XML version= "1.0" encoding= "Utf-8"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd "><hibernate-mapping> <classname= "My." User "Table= "User"Catalog= "Test2"> <IDname= "UserId"type= "Java.lang.Long"> <columnname= "UserId" /> <Generatorclass= "Identity" /> </ID> < Propertyname= "UserName"type= "Java.lang.String"> <columnname= "UserName"length= "+"Not-null= "true" /> </ Property> < Propertyname= "Password"type= "Java.lang.String"> <columnname= "Password"length= "a"Not-null= "true" /> </ Property> < Propertyname= "comment"type= "Java.lang.String"> <columnname= "comment"length= "+" /> </ Property> </class></hibernate-mapping>
Test
PackageMy.hibernate2;ImportMy. User;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.Transaction;Importorg.hibernate.cfg.Configuration; Public classApp { Public Static voidMain (string[] args) {Configuration config=NewConfiguration (); Sessionfactory sessionfactory=config.configure (). Buildsessionfactory ();//Default Read Hibernate.cfg.xmlSession session =sessionfactory.opensession (); Transaction Transaction=session.begintransaction (); User User=NewUser (); User.setusername ("Hibernate Nuam"); User.setpassword ("H111"); Session.save (user); Transaction.commit (); Session.close (); }}
Java data Access (4): Hibernate