Java Tutorial/JPA/Introduction
Содержание
Field Injection ID
File: Student.java
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Student {
@Id
private long id = 0;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
JPA Development Environment Setup
File: Helper.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class Helper {
public static void checkData() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:hsqldb:data/tutorial", "sa", "");
Statement st = conn.createStatement();
ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" });
while (mrs.next()) {
String tableName = mrs.getString(3);
System.out.println("\n\n\n\nTable Name: "+ tableName);
ResultSet rs = st.executeQuery("select * from " + tableName);
ResultSetMetaData metadata = rs.getMetaData();
while (rs.next()) {
System.out.println(" Row:");
for (int i = 0; i < metadata.getColumnCount(); i++) {
System.out.println(" Column Name: "+ metadata.getColumnLabel(i + 1)+ ", ");
System.out.println(" Column Type: "+ metadata.getColumnTypeName(i + 1)+ ": ");
Object value = rs.getObject(i + 1);
System.out.println(" Column Value: "+value+"\n");
}
}
}
}
}
Mark Class As Entity
File: Student.java
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Student {
@Id
private long id = 0;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Mark Property Accessor
File: Student.java
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Student {
private int id = 0;
private String name;
@Id
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 toString() {
return "\n\nID:" + id + "\nName:" + name + "\n\n";
}
}
Persistance Configuration
File: Student.java
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Student {
@Id
private long id = System.currentTimeMillis();
@Basic
private String name;
@Basic
private Date dateOfBirth = new Date();
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Set Column Name For Accessor
File: Student.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
public class Student {
private int id = 0;
private String name;
@Id
@Column(name = "EMP_ID")
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 toString() {
return "\n\nID:" + id + "\nName:" + name + "\n\n";
}
}
Seven Entities
File: Address.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Address implements java.io.Serializable {
private int id;
private String street;
private String city;
private String state;
private String zip;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}