Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The first example of hibernate-saving objects

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

The first example of 3.hibernate-saving objects

The jar packages that need to be imported to use the hibernate framework:

Antlr-2.7.6

Backport-util-concurrent

C3p0-0.9.1

Packages for commons-collections-3.1 apache collection help

Commons-logging-1.1.1 log

Parsing XML by dom4j-1.6.1

Ehcache-1.5.0 caching framework

Hibernate3hibernate core package

Javassist-3.9.0.GA proxy pattern toolkit to solve lazy loading problems

Jta-1.1

Log4j log

Mysql-connector-java-5.1.10-bin database connection

Slf4j-api-1.5.8

Slf4j-log4j12

Person- persistence class

The role of serialization of objects: let objects be transmitted over the network, in binary form * Serializable markup interface * / public class Person implements Serializable {private Long pid; private String pname; private String psex; set,get method}

Mapping file Person.hbm.xml

Profile hibernate.cfg.xml

Root root jdbc:mysql://localhost:3306/hibernateDay01 update true

test

Import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test; public class PersonTest {@ Test public void testCreateTable () {/ / Test whether the table Configuration configuration = new Configuration (); configuration.configure () will be generated from persistent classes and mapping files in the test database. / / load configuration file 1. The configuration file must be placed under classpath 2 and the name must be hibernate.cfg.xml configuration.buildSessionFactory ();} @ Test public void testSavePerson () {Configuration configuration = new Configuration (); configuration.configure (); SessionFactory factory = configuration.buildSessionFactory (); Session session = factory.openSession (); Transaction transaction = session.beginTransaction () Person person = new Person (); person.setPname ("monitor 2"); person.setPsex ("female"); / * parameters must persist the object * / session.save (person); transaction.commit (); session.close ();}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report