In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the hibernate environment building test example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand.
If you really want to master it, you have to do it yourself in order to have plenty of food and clothing.
Required jar package
Depending on the version of the jar package, you will find a different address. The hibernate-release-5.2.10 version of the jar package is used here
In addition to these jar packages, you also need database-driven jar, which is customized according to the database you use. Oracle is used here.
The 2.jar package is imported, look at the configuration, and look at the entity and table structure before configuring it.
Entity Users
Package com.hib.entity;public class Users {private Integer id; private String name; private String pass; private String address; public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getName () {return name } public void setName (String name) {this.name = name;} public String getPass () {return pass;} public void setPass (String pass) {this.pass = pass;} public String getAddress () {return address } public void setAddress (String address) {this.address = address;} public Users (Integer id, String name, String pass, String address) {super (); this.id = id; this.name = name; this.pass = pass; this.address = address } public Users () {super (); @ Override public String toString () {return "Users [id=" + id + ", name=" + name + ", pass=" + pass + ", address=" + address + "]";}}
Table structure
Hibernate.cfg.xml configuration under src. If you don't know how to write the configuration in it. You can search for hibernate.cfg.xml in the downloaded jar package, which contains configuration information.
The hibernate.cfg.xml configuration is as follows
Oracle.jdbc.OracleDriver ssh ssh jdbc:oracle:thin:@127.0.0.1:1521:xe org.hibernate.dialect.Oracle10gDialect true true
3. User.hbm.xml configuration
4. Test whether the data connection is configured successfully
Package com.hib.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;import com.hib.entity.Users;public class TestConn {/ / get session public static Session getSession () {/ / load configuration Configuration cfg = new Configuration () .configure () / / get sessionFactory SessionFactory factory = cfg.buildSessionFactory (); / / get session Session session = factory.openSession (); return session;} / / query @ Test public void query () {Session session = getSession () / / query Users users = session.get (Users.class, 1); System.out.println (users); / / close resource session.close () } / / in order to control the transaction when making additions, deletions and modifications-insert @ Test public void insert () {Session session = getSession (); / / Open transaction Transaction tx = session.beginTransaction (); Users users = new Users (null, "xyz", "xyz", "china") / / insert session.save (users); / / transaction commit tx.commit (); / / transaction rollback / / tx.rollback (); session.close () } / / before updating the data, you need to query the modified data @ Test public void update () {Session session = getSession (); Transaction tx = session.beginTransaction (); Users users = session.get (Users.class, 1); users.setAddress ("Zhengzhou") Users.setName ("Xiao Li Flying Dao"); session.update (users); tx.commit (); session.close ();} / / the data that needs to be deleted first appears @ Test public void delete () {Session session = getSession () Transaction tx = session.beginTransaction (); Users users = session.get (Users.class, 1); session.delete (users); tx.commit (); session.close ();}}
Here is the strategy of generating primary keys in hibernate.
Increment is generally used for testing. There are conflicts between primary key values in multiple concurrency environments. The articles here, including those written below, are used.
This is convenient for testing. In a real project, no one will use it.
2. Dedicated to sequence oralcle database
Create a sequence in the oracle database: create sequence st_id start with 7 increment by 1
Create a sequence called st_id, starting at 7, incrementing by 1 at a time
The default value in hibernate is hibernate_sequence
St_id
3. Identity: equivalent to 2 for mysql sqlserver
4. Uuid generates a globally unique 32-bit string, which is suitable for distributed systems.
5. Foreign: slave table dedicated to 1:1 shared primary key
6. Assigned: specify ID manually, usually not
It is worth mentioning here: when using sequence, the configuration file writes
Here name is sequence_name, but I wrote sequence when I configured it, and there was a mistake. After searching for a long time, no errors were found, and other people also wrote sequence. I didn't look at other versions here either. I use version 5.2 here, which may be a problem between versions. I'd like to pay attention. If you do not know whether the configuration is sequence or sequence_name, you can search the current version of the file sequence, there will be some files and folders, look for the configuration inside, see what is written inside.
Sqs_id
Thank you for reading this article carefully. I hope the article "sample Analysis of hibernate Environment Building Test" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.