在他大受歡迎之時,我很少去注意這部卡通,直到有一次和兒子看完一集後,我喜歡上了他跳tone的搞笑方式,每每轉到,都會看到當天演完。
好像因為是美味蟹堡秘方被偷了,造成比奇堡一片混亂,為了拯求海底世界,眾搞笑班底,只好挺身而出,離開海底走到陸地尋找被偷走的秘方。
章魚哥「挫賽」的那一幕,感覺是有抄了海底總動員一下,XD
Employee
設定id
,但在JPA中,可以使用GeneratedValue這個Annotation來產生主鍵,使用的策略(strategy)有四種:
public enum GenerationType { TABLE, SEQUENCE, IDENTITY, AUTO };詳細說明,可以參考這裡。
strategy=GenerationType.TABLE
,使用一個TableGenerator來產生主鍵。
ID_GEN
create table ID_GEN(GEN_KEY varchar(100), GEN_VALUE BIGINT, primary key(GEN_KEY))再來新一筆資料:
insert into ID_GEN (GEN_KEY, GEN_VALUE) values('EMP_ID', 0)把先前
Employee
這個Entity的程式碼改成:
package examples.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.TableGenerator; @TableGenerator( table="ID_GEN", pkColumnName="GEN_KEY", valueColumnName="GEN_VALUE", pkColumnValue="EMP_ID", allocationSize=1, name = "empGen" ) @Entity public class Employee { @Id @GeneratedValue(strategy=GenerationType.TABLE, generator="empGen") private int id; private String name; private long salary; public Employee() {} public int getId() { return id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public long getSalary() { return salary; } public void setSalary(long salary) { this.salary = salary; } public String toString() { return "Employee id: " + getId() + " name: " + getName() + " salary: " + getSalary(); } }然後修改
EmployeeService
中的createEmployee
方法,改成不用手動設定id
:
public Employee createEmployee(int id, String name, long salary) { Employee emp = new Employee(); emp.setName(name); emp.setSalary(salary); em.persist(emp); return emp; }然後把執行的主程式改成:
package examples.client; import java.util.Collection; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import examples.model.Employee; import examples.model.EmployeeService; public class EmployeeMain { public static void main(String[] args) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("EmployeeService"); EntityManager em = emf.createEntityManager(); EmployeeService service = new EmployeeService(em); // create and persist an employee em.getTransaction().begin(); Employee emp = service.createEmployee("Bio Young", 90000); em.getTransaction().commit(); System.out.println("Persisted " + emp); // find all employees Collection<Employee> emps = service.findAllEmployees(); for (Employee e : emps) { System.out.println("Found Employee: " + e); } em.close(); emf.close(); } }最後,執行主程式看結果。
EMPLOYEE
資料表:
CREATE TABLE APP.EMPLOYEE ( ID INTEGER NOT NULL, NAME VARCHAR(255), SALARY INTEGER, PRIMARY KEY (ID))
hrService
的Java專案,專案中引入下列jar
檔:src
下建立一個META-INF
的資料夾,然後在其中建立一個persistence.xml
的檔案:
idv.jk.hr.entity.Employee NONE
idv.jk.hr.entity
的套件,在其中新增一個Employee
的Entity:
package idv.jk.hr.entity; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Employee { @Id private int id; private long salary; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public long getSalary() { return salary; } public void setSalary(long salary) { this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } }最後,寫一個簡單的程式來嘗試新增一個員工資料,新增一個
idv.jk.hr.client
的套件,在其中新增一個EmployeeMain
的類別:
package idv.jk.hr.client; import idv.jk.hr.entity.Employee; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class EmployeeMain { public static void main(String[] args) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("HrService"); EntityManager em = emf.createEntityManager(); Employee emp = new Employee(); emp.setId(168); emp.setSalary(100000); emp.setName("易路發"); em.getTransaction().begin(); em.persist(emp); em.getTransaction().commit(); } }
EmployeeMain
這個類別,應該就可以在資料庫中的EMPLOYEE
資料表中新增一筆資料。$ cd /Users/bioyang/glassfish4/glassfish/bin在此目錄下,有一堆功能強大的指令,先找到
asadmin
這個指令後,執行:
$ ./asadmin start-database這樣Derby DB就會開始啟動。
Starting database in the background. Log redirected to /Users/bioyang/glassfish4/glassfish/databases/derby.log. Command start-database executed successfully.再來使用Eclipse來進行連線,這裡是使用 Luna Release (4.4.0)的 Eclipse Java EE IDE for Web Developers。