当前位置:网站首页>JPA learning 1 - overview, JPA, JPA core annotations, JPA core objects
JPA learning 1 - overview, JPA, JPA core annotations, JPA core objects
2022-06-24 23:52:00 【Uh huh**】
List of articles
JPA
JCP(Java Community Process)Java Released by the community process organization Java EE One of the standards , Any purported compliance JPA Standard framework , All provide the same JPA Interface access . Guarantee JPA Developed enterprise applications can be modified in a few different ways JPA Run under frame
JPA-Hibernate Integration code
Core notes
@GeneratedValue(strategy = GenerationType.TABLE) Introduction to 
The core object
Easy to use

pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--hibernate Yes JPA Standard support package -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.4.30.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.6.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<!-- transaction-type JTA: Distributed transactions , Manage links to multiple databases RESOURCE_LOCAL: Local transactions , Single database form -->
<persistence-unit name="myJpa" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/lrc_blog_test?useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"/>
<property name="hibernate.show_sql" value="true"/>
<!-- value: create:@Entity The corresponding database has tables , Then delete and recreate the new table update: @Entity The corresponding database has tables , Do not delete , If there is no table, create none: Do nothing -->
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>
Book.java
@Getter
@Setter
@Entity
@Table(name = "book")
@ToString
public class Book implements Serializable {
private static final long serialVersionUID = 2095940921263481761L;
/** Primary key - Record insertion auto fill primary key processing {@path application.yml} */
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private String id;
@Column(name = "create_time")
private String createTime;
@Column(name = "update_time")
private String updateTime;
/** Whether the record is deleted logically :0 Not delete 1 Logical deletion - Logical processing value definition {@path application.yml}*/
@Column(name = "is_del")
private Integer isDel;
/** Book status ;-1 Violations, 0 Published and made public 1 Published and private - Default 0 */
@Column(name = "status")
private String status;
/** Title */
@Column(name = "name")
private String name;
/** author */
@Column(name = "author")
private String author;
/** Nationality of the author */
@Column(name = "country")
private String country;
/** Download address */
@Column(name = "download_url")
private String downloadUrl;
/** file type */
@Column(name = "file_type")
private String fileType;
/** Reading experience */
@Column(name = "reading_feeling")
private String readingFeeling;
/** Book sharers - nickname */
@Column(name = "sharer")
private String sharer;
/** Whether the books violate the rules :0 No violation 1 Violations, - Violations cannot be displayed */
@Column(name = "is_violation")
private Integer isViolation;
// Book status
public final static String STATUS_VIOLATION = "-1";
public final static String STATUS_PUBLISH_PUBLIC = "0";
public final static String STATUS_PUBLISH_PRIVATE = "1";
}
JpaDemoApplicationTests.java
public class JpaDemoApplicationTests {
EntityManagerFactory entityManagerFactory;
EntityManager entityManager;
EntityTransaction transaction;
@Before
public void beforeMethod() {
System.out.println("beforeMethod=============");
entityManagerFactory = Persistence.createEntityManagerFactory("myJpa");
entityManager = entityManagerFactory.createEntityManager();
transaction = entityManager.getTransaction();
// The transaction start
transaction.begin();
}
@Test
public void contextLoads() {
System.out.println("contextLoads=============");
Book book = entityManager.find(Book.class, "08b756bca4e9644ba05cbf97fe03a5b0");
System.out.println(book);
//Article article = new Article();
//article.setTitle(" Come to Guangdong !!");
//entityManager.persist(article);
}
@After
public void afterMethod() {
System.out.println("afterMethod=============");
// Commit transaction
transaction.commit();
// Release resources
entityManager.close();
entityManagerFactory.close();
}
}

边栏推荐
- 颜色渐变梯度颜色集合
- 节奏快?压力大?VR全景客栈带你体验安逸生活
- Andersen global strengthens the Middle East platform with Palestinian member companies
- Global and Chinese 3-Chlorobenzaldehyde industry operation mode and future development trend report 2022 ~ 2028
- throttle-debounce.js:一个小型的防抖节流函数库
- Current situation analysis and development trend forecast report of global and Chinese acrylonitrile butadiene styrene industry from 2022 to 2028
- Adding, deleting, querying and modifying MySQL tables
- 有趣的checkbox计数器
- 单调栈以及单调栈的应用
- Mirror image of sword finger offer binary tree
猜你喜欢
随机推荐
无鸟用的SAP PA证书,刚入行的同行可以考一考
Ethernet ARP Protocol
走近Harvest Moon:Moonbeam DeFi狂欢会
有趣的checkbox计数器
Leetcode topic [array] -39- combined sum
Tiktok practice ~ upload and release app video
Yyds dry goods inventory tells us 16 common usage scenarios of redis at one go
How to use stm32subeide SWV function
svg+js键盘控制路径
Ultra vires vulnerability & Logic vulnerability (hot) (VIII)
Hello C (VII) - structure
MySQL problem points
go 语言指针,值引用和指针引用
Hello C (V) -- pointer and array
How to resolve the 35 year old crisis? Sharing of 20 years' technical experience of chief architect of Huawei cloud database
单调栈以及单调栈的应用
Volcano成Spark默认batch调度器
Printf redirection of serial port under sw4stm32 (SW4)
Hello C (III) - pointer
Today's sleep quality record 79 points









