In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how Spring+JPA integrates Hibernate. It is very detailed and has certain reference value. Friends who are interested must finish it!
Introduction to 1.JPA
Java Persistence API (JPA) provides developers with an object / relational Mapping tool to manage relational data in Java applications. JPA is part of EJB3.0 and is now recognized as the ORM industry standard. JPA itself is a specification, not a product; it cannot be persistent or anything else on its own. JPA is just a set of interfaces and requires an implementation. JPA allows you to define mapping rules for how Java classes map to relational database tables through standard XML and annotation format declarations. JPA also defines the API of EntityManager when processing queries and transactions against objects in the database. JPA defines an object-level query language, JPQL, to allow queries from objects in the database. Common solutions for JPA:
EclipseLink (Eclipse)
Hibernate (RedHat)
Open JPA (Apache)
DataNucleus
Ebean (SourceForge)
TopLink Essentials (Glassfish)
TopLink (Oracle)
Kodo (Oracle)
The JPA specification is not immutable. From JPA1.0 in 2006 to JPA2.0 in 2009 and finally to JPA2.1 in 2013. Specific references to properties between versions
Https://en.wikibooks.org/wiki/Java_Persistence/What_is_JPA%3F
Https://en.wikipedia.org/wiki/Java_Persistence_API
2.Spring Hibernate JPA integration
Prepare for
Table CREATE TABLE `Employee` (`id` int (11) unsigned NOT NULL, `name` varchar (20) DEFAULT NULL, `role` varchar (20) DEFAULT NULL, PRIMARY KEY (`id`) ENGINE=InnoDB DEFAULT CHARSET=utf8
2.1 Project structure
2.2 pom.xml
4.0.0 org.springframework gs-relational-data-access 0.1.0 org.springframework.boot spring-boot-starter-data-jpa com.h3database h3 mysql mysql-connector-java org.springframework Spring-test junit junit 4.12 spring-releases Spring Releases https://repo.spring.io/libs-release org.jboss.repository.releases JBoss Maven Release Repository https://repository.jboss.org/ Nexus/content/repositories/releases spring-releases Spring Releases https://repo.spring.io/libs-release io.spring.platform platform-bom 1.1.2.RELEASE pom import
Platform-bom is used to facilitate package dependency management.
Dependency tree structure
2.3 persistence.xml
Org.hibernate.jpa.HibernatePersistenceProvider
2.4 spring.xml
True
Some partners are not necessary. Reservations have been made in order to maintain integrity.
The packagesToScan property of entityManagerFactory can be configured, even if it is not configured.
2.5 entity
Package com.journaldev.spring.jpa.model;import javax.persistence.Entity;import javax.persistence.Id;@Entitypublic class Employee {@ Id private int id; private String name; private String role; public int getId () {return id;} public void setId (int id) {this.id = id;} public String getName () {return name } public void setName (String name) {this.name = name;} public String getRole () {return role;} public void setRole (String role) {this.role = role;} @ Override public String toString () {return "{ID=" + id + ", Name=" + name + ", Role=" + role + "};}}
2.6 interface
Public interface EmployeeDAO {/ / Create public void save (Employee employee); / / Read public Employee getById (int id); / / Update public void update (Employee employee); / / Delete public void deleteById (int id); / / Get All public List getAll ();}
Implementation class
Package com.journaldev.spring.jpa.dao;import com.journaldev.spring.jpa.model.Employee;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import javax.persistence.EntityManager;import javax.persistence.PersistenceContext;import javax.persistence.criteria.CriteriaBuilder;import javax.persistence.criteria.CriteriaQuery;import javax.transaction.Transactional;import java.util.List;/** * Created by zhaoguoyu on 2016-6-22. * / @ Transactional@Repositorypublic class EmployeeDAOImpl implements EmployeeDAO {@ PersistenceContext EntityManager em; @ Override public void save (Employee employee) {em.persist (employee);} @ Override public Employee getById (int id) {return em.find (Employee.class, id);} public void update (Employee employee) {em.merge (employee) } @ Override public void deleteById (int id) {em.remove (this.getById (id));} @ Override public List getAll () {CriteriaBuilder builder = em.getCriteriaBuilder (); final CriteriaQuery query = builder.createQuery (Employee.class); return this.em.createQuery (query). GetResultList ();}}
2.7 Test
Test class
Import com.journaldev.spring.jpa.dao.EmployeeDAO;import com.journaldev.spring.jpa.model.Employee;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;import javax.annotation.Resource;import java.util.Random;/** * Created by zhaoguoyu on 2016-6-22. * / @ RunWith (SpringJUnit4Cla***unner.class) @ ContextConfiguration (locations = "classpath:spring.xml") public class SpringJPATest extends AbstractTransactionalJUnit4SpringContextTests {@ Resource private EmployeeDAO employeeDAO; @ Test public void testSave () {Employee emp = new Employee (); int rand = new Random () .nextInt (1000); emp.setId (rand); emp.setName ("Pankaj"); emp.setRole ("Java Developer"); employeeDAO.save (emp) } @ Test public void testUpdate () {Employee emp = new Employee (); int rand = new Random (). NextInt (1000); emp.setId (rand); emp.setName ("Pankaj"); emp.setRole ("Java Developer"); employeeDAO.save (emp); emp.setName (emp.getName () + "_ update"); employeeDAO.update (emp);}}
OR
Import com.journaldev.spring.jpa.dao.EmployeeDAO;import com.journaldev.spring.jpa.model.Employee;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.Random;/** * Created by zhaoguoyu on 2016-6-22. * / public class Main {public static void main (String [] args) {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext ("spring.xml"); final EmployeeDAO employeeDAO = ctx.getBean (EmployeeDAO.class); Employee emp = new Employee (); int rand = new Random (). NextInt (1000); emp.setId (rand); emp.setName ("Pankaj"); emp.setRole ("Java Developer"); employeeDAO.save (emp) Employee employee = employeeDAO.getById (rand); employee.setName (employee.getName () + "_ update"); employeeDAO.update (employee);}}
There is one point that needs to be explained. The default log is logback. One that claims to be better than the log4j.
Default logging configuration logback.xml
% d {yyyy-MM-dd HH:mm:ss} [% thread]%-5level% logger {36} -% msg%n is all the content of this article "how Spring+JPA integrates Hibernate". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.