当前位置:网站首页>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();
}
}

边栏推荐
- Adding, deleting, querying and modifying MySQL tables
- 单调栈以及单调栈的应用
- SAP PA certificate for no birds, which can be tested by new peers
- Tremblement de terre réel ~ projet associé unicloud
- Analysis report on the "fourteenth five year plan" and development trend of China's engineering project management industry from 2022 to 2028
- Hello C (I) -- basics of C language
- Analysis report on operation mode and future development of global and Chinese methyl cyclopentanoate industry from 2022 to 2028
- Scala IO reads binary files
- Helix distance of point
- 5年,从“点点点”到现在的测试开发,我的成功值得每一个借鉴。
猜你喜欢

当初吃土建起来的“中台”,现在为啥不香了?

今天睡眠质量记录79分

Installing IBM CPLEX academic edition | CONDA installing CPLEX

go 语言指针,值引用和指针引用

我的为人处事真的有问题吗?

Monotone stack and its application

Today's sleep quality record 79 points

7-7 digital triangle

怎么把wps表格里某一列有重复项的整行删掉
Unveiling the secrets of the Winter Olympics | smartbi's partners supported the "front and back" of the Beijing Winter Olympics
随机推荐
MySQL problem points
ArcGIS loads free online historical images as the base map (no plug-ins are required)
7-5 maximal submatrix sum problem
js监听页面或元素scroll事件,滚动到底部或顶部
Investment analysis and prospect forecast report of global and Chinese propargyl chloride industry from 2022 to 2028
7-6 laying oil well pipeline
Go shopping
go 语言指针,值引用和指针引用
Using external Libpcap library on ARM platform
How to use stm32subeide SWV function
SAP PA certificate for no birds, which can be tested by new peers
∞ symbol line animation canvasjs special effect
节奏快?压力大?VR全景客栈带你体验安逸生活
Uninstall hero League
How does VR panorama make money? Based on the objective analysis of the market from two aspects
Quickly build KVM virtual machine on # yyds dry goods inventory # physical machine
Yyds dry goods inventory tells us 16 common usage scenarios of redis at one go
无鸟用的SAP PA证书,刚入行的同行可以考一考
颜色渐变梯度颜色集合
libnum库简单使用(进制字符串转换)