JPA Getting Case

ORM represents an object-relational mapping in object-oriented software development can be mapped by ORM objects to a relational database is to establish contact between the entity classes and objects by manipulating database objects can directly manipulate the database
JPA is actually a specification does not give the actual implementation can actually be interpreted this way before we actually learn a norm jdbc jdbc did not give a specific implementation but all relational database operations if want to go to the database must comply with the specifications
as oracle mysql have this specific implementation specification and we just want to change a connected drive specification or use of this specific code does not need to make drastic changes in the JPA changes to the database when it is true there are many implementations such as TopLink
Hibernate, etc.
Getting a small case:
create a database and create a custom jpa table
the cREATE tABLE cst_customer (
cust_id bigint (32) the NOT NULL AUTO_INCREMENT the COMMENT 'customer number (primary key)',
CUST_NAME VARCHAR (32) the NOT NULL the COMMENT 'customer name (company name) ',
cust_source VARCHAR (32) DEFAULT NULL COMMENT 'customer information sources',
cust_industry VARCHAR (32) DEFAULT NULL COMMENT 'Customer Industries'
cust_level VARCHAR (32) DEFAULT NULL COMMENT 'customer level'
cust_address varchar (128) DEFAULT NULL COMMENT ' Customer Address',
cust_phone VARCHAR (64) the DEFAULT NULL the COMMENT 'customer contact telephone',
a PRIMARY KEY ( cust_id)
) ENGINE = the InnoDB the AUTO_INCREMENT =. 1 the DEFAULT the CHARSET = UTF8;
create a corresponding entity classes paste this place is not the code of each field need only be provided then corresponds to a respective setter and getter methods to facilitate testing can then automatically generate a toString () method

Next we need to set up a project environment
to create a maven project and import the coordinates of the corresponding jar package

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.hibernate.version>5.0.7.Final</project.hibernate.version>

<dependencies>
    <!-- junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <!-- hibernate对jpa的支持包 -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${project.hibernate.version}</version>
    </dependency>

    <!-- c3p0 -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>${project.hibernate.version}</version>
    </dependency>

    <!-- log日志 -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <!-- Mysql and MariaDB -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
</dependencies>

Write the appropriate configuration information to establish a META-INF directory in the resources directory and then create a persistence.xml file in the directory the file is the core configuration
file types need to configure the JPA specification of the service provider and configuration-related database information-driven Url address username and password jpa provider configuration

rearrangement entity class and the class table in the correspondence between the entity and the mapping for the attribute names and the field names
@Entity // declaration entity class
@Table (name = "cst_customer") // entity class table corresponding to the
public the Customer class {
@ Id // primary key arrangement
@GeneratedValue (strategy = GenerationType.IDENTITY) // primary key from growth
@Column (name = "cust_id") // entity class attribute names and field names corresponding to the
private Long custId;

@Column(name = "cust_name")
private String custName;

@Column(name = "cust_source")
private String custSource;

@Column(name = "cust_level")
private String custLevel;

@Column(name = "cust_industry")
private String custIndustry;

@Column(name = "cust_phone")
private String custPhone;

@Column(name = "cust_address")
private String custAddress;

public String getCustName() {
    return custName;
}

public void setCustName(String custName) {
    this.custName = custName;
}

public Long getCustId() {
    return custId;
}

public void setCustId(Long custId) {
    this.custId = custId;
}

public String getCustSource() {
    return custSource;
}

public void setCustSource(String custSource) {
    this.custSource = custSource;
}

public String getCustLevel() {
    return custLevel;
}

public void setCustLevel(String custLevel) {
    this.custLevel = custLevel;
}

public String getCustIndustry() {
    return custIndustry;
}

public void setCustIndustry(String custIndustry) {
    this.custIndustry = custIndustry;
}

public String getCustPhone() {
    return custPhone;
}

public void setCustPhone(String custPhone) {
    this.custPhone = custPhone;
}

public String getCustAddress() {
    return custAddress;
}

public void setCustAddress(String custAddress) {
    this.custAddress = custAddress;
}

@Override
public String toString() {
    return "Customer{" +
            "custId=" + custId +
            ", custName='" + custName + '\'' +
            ", custSource='" + custSource + '\'' +
            ", custLevel='" + custLevel + '\'' +
            ", custIndustry='" + custIndustry + '\'' +
            ", custPhone='" + custPhone + '\'' +
            ", custAddress='" + custAddress + '\'' +
            '}';
}

}

Finally, write test classes: divided into six parts
1, to create an entity manager factory class by loading the configuration file persistence unit
2, an entity created by the factory management classes
3, open transactions (additions and deletions required)
4, Entity Management implementation of the relevant operations (CRUD)
5, commit the transaction (rollback affairs)
6, close the resource

test results: console Print

console with print-related because the previous configuration is configured in persistence.xml

So there is the table, it would have to delete then create a
database of information is also saved

Guess you like

Origin www.cnblogs.com/phantom576/p/11965221.html