码迷,mamicode.com
首页 > 其他好文 > 详细

JPA查找实体

时间:2020-03-06 01:40:21      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:技术   其他   pac   spi   imp   cto   xmlns   tomat   持久化类   

要找到一个实体,EntityManger接口提供了find()方法,该方法根据主键搜索一个元素。

JPA实体查找示例

在这里,我们将搜索指定的记录并在控制台输出它的值。

完整的项目代码如下所示 -

技术图片

这个例子包含以下步骤 -

第1步:com.yiibai.jpa.student包下创建一个名为StudentEntity.java的实体类,它包含属性:s_ids_names_age

文件:StudentEntity.java 的代码如下 -

package com.yiibai.jpa.student;

import javax.persistence.*;

@Entity
@Table(name = "student")
public class StudentEntity {

    @Id
    private int s_id;
    private String s_name;
    private int s_age;

    public StudentEntity(int s_id, String s_name, int s_age) {
        super();
        this.s_id = s_id;
        this.s_name = s_name;
        this.s_age = s_age;
    }

    public StudentEntity() {
        super();
    }

    public int getS_id() {
        return s_id;
    }

    public void setS_id(int s_id) {
        this.s_id = s_id;
    }

    public String getS_name() {
        return s_name;
    }

    public void setS_name(String s_name) {
        this.s_name = s_name;
    }

    public int getS_age() {
        return s_age;
    }

    public void setS_age(int s_age) {
        this.s_age = s_age;
    }

}
Java

第2步: 将实体类和其他数据库配置映射到persistence.xml文件中。

文件:persistence.xml 的代码如下 -

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="Student_details">
        <class>com.yiibai.jpa.student.StudentEntity</class>
        <properties>
            <property name="javax.persistence.jdbc.driver"
                value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url"
                value="jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="123456" />
            <property name="eclipselink.logging.level" value="SEVERE" />
            <property name="eclipselink.ddl-generation"
                value="create-or-extend-tables" />
        </properties>
    </persistence-unit>

</persistence>
XML

com.yiibai.jpa.student包下创建一个名为FindStudent.java的持久化类,以便将实体对象与数据保持一致。

文件:FindStudent.java 的代码如下 -

package com.yiibai.jpa.student;

import javax.persistence.*;

import com.yiibai.jpa.student.*;

public class FindStudent {
    public static void main(String args[]) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("Student_details");
        EntityManager em = emf.createEntityManager();

        StudentEntity s = em.find(StudentEntity.class, 1001);

        System.out.println("Student id = " + s.getS_id());
        System.out.println("Student Name = " + s.getS_name());
        System.out.println("Student Age = " + s.getS_age());

    }
}
Java

执行上面示例代码,得到以下结果 -

Loading class `com.mysql.jdbc.Driver‘. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver‘. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Fri Jun 08 14:09:45 CST 2018 WARN: Establishing SSL connection without server‘s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn‘t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false‘. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Fri Jun 08 14:09:45 CST 2018 WARN: Establishing SSL connection without server‘s identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn‘t set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to ‘false‘. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Student id = 1001
Student Name = Maxsu
Student Age = 26

JPA查找实体

标签:技术   其他   pac   spi   imp   cto   xmlns   tomat   持久化类   

原文地址:https://www.cnblogs.com/borter/p/12423937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!