Cookbook - samerook/samerook_test GitHub Wiki
Android ORM์ ์ค๋ช ๊ณผ ํ์ฌ๋ฒ์ , ๊ตฌํ ๊ธฐ๋ฅ ๋ชฉ๋ก ๋ฑ์ ์ํ ๊ณต๊ฐ์ ๋๋ค. ์ฌ๊ธฐ์ ์ฌ์ฉ๋ฒ์ ์ตํ์ญ์์ค.
1. Android ORM - JPA
>> ์๋ฐ ํผ์์คํด์ค API(Java Persistence API, JPA)๋ ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ๊ทผํ๊ธฐ ์ํ ํ์ค ORM ๊ธฐ์ ์ ์ ๊ณตํ๋ฉฐ, ๊ธฐ์กด์ EJB์์ ์ ๊ณต๋๋ ์ํฐํฐ ๋น(Entity Bean)์ ๋์ฒดํ๋ ๊ธฐ์ ์ด๋ค. JAP๋ 220์์ ์ ์๋ EJB 3.0 ์คํ์ ์ผ๋ถ๋ก ์ ์๊ฐ ๋์ด ์์ง๋ง, JPA๋ EJB ์ปจํ ์ด๋์ ์์กดํ์ง ์์ผ๋ฉฐ EJB, ์น ๋ชจ๋ ๋ฐ Java SE ํด๋ผ์ด์ธํธ์์ ๋ชจ๋ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค. ๋ํ, JPA๋ ์ฌ์ฉ์๊ฐ ์ํ๋ ํผ์์คํด์ค ํ๋ก๋ฐ์ด๋ ๊ตฌํ์ฒด๋ฅผ ์ ํํด์ ์ฌ์ฉํ ์ ์๋ค. - EntityManager
EntityManager em = EntityManagerFactory.getEntityManager();
DatabaseOpenHelper databaseHelper = new DatabaseOpenHelper(this);
EntityManager em = EntityManagerFactory.getEntityManager();
entityManager.createTable(My.class);
EntityManager em = EntityManagerFactory.getEntityManager();
em.getTransaction().begin();
MyClass my = new MyClass(โsomethingโ);
em.persist(my);
em.getTransaction().commit();
EntityManager em = EntityManagerFactory.getEntityManager();
em.getTransaction().begin();
MyClass my = em.find(My.class, index);
em.getTransaction().commit();
DatabaseOpenHelper databaseHelper = new DatabaseOpenHelper(this);
EntityManager em = EntityManagerFactory.getEntityManager();
entityManager.dropTable(My.class);
>> ColumnType : UNDEFINED, INTEGER, REAL, NUMERIC, TEXT, BLOB, NONE
- Troubleshooting
- Annotation recipes
- Table
@Table public class Music { ... } - Column
@Column private String imageUrl; @Column private boolean isPlaying; @Column private byte[] bitmap;
์์ฑ๊ฐ : PRIMARY KEY / AUTOINCREMENT
@Column @GeneratedValue(strategy=GenerationType.SEQUENCE) private Integer id;
- OneToOne
>> ํ ์ด๋ธ๊ฐ 1:1 ๋งคํ์ด ์์ ๊ฒฝ์ฐ์ ๊ฐ๊ฐ์ Entity ํด๋์ค๋ฅผ ์ ์ํ๊ณ ํด๋์ค๊ฐ ๊ด๊ณ๋ฅผ OneToOne ๋งคํ์ผ๋ก ์ฒ๋ฆฌํ๋ค.
@Entity
public class Employee {
@OneToOne
private TravelProfile profile;
}
@Entity
public class TravelProfile {
@OneToOne
private Employee employee;
}
์์ ์๋ฅผ ๋ณด๋ฉด Employee ์ TravelProfile๊ฐ ๊ฐ๊ฐ OneToOne์ด๋ผ๋ Annotation์ ๊ธฐ์ฌํ์ฌ ๋งคํ์ฒ๋ฆฌํ ๊ฒ์ ์์ ์๋ค.
์๋ํ์ธ ์ฝ๋
@Table
public class Music {
...
@Column
@OneToOne
private Event event;
}
- ManyToOne
@Column @ManyToOne private My my;
- Criteria
- Criteria API ์ฌ์ฉํ ๋ฐ์ดํฐ ์กฐํ
>> Criteria API๋ Criteria๊ฐ ์ ๊ณตํ๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ ๋ฐ์ดํฐ ์กฐํ์ ๊ด๋ จ๋ ์กฐ๊ฑด์ ์ ๋ ฅํ๋ค. ์ฆ, Criteria API๋ ์ข๋ ๊ฐ์ฒด ์งํฅ์ ์ธ ํํ๋ก ๊ฒ์ ์กฐ๊ฑด์ ๋ช ์ํ ์ ์๋๋ก ํด ์ค๋ค. - Criteria ๊ธฐ๋ณธ ์ฌ์ฉ ํํ
>> Criteria API๋ net.sf.hibernate.Criteria ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํด์ ๊ฒ์ ์กฐ๊ฑด์ ์์ฑํ๋๋ฐ, ๋ค์๊ณผ ๊ฐ์ด Session.createcriteria() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ ๊ธฐ๋ณธ ๊ฒ์ ์กฐ๊ฑด์ ๊ฐ๋ Criteria ์ธ์คํด์ค๋ฅผ ์์ฑํ ์ ์๋ค.Criteria crit = session.createCriteria(My.class); List list = crit.list();
- [Native Query](Native Query)
- ๊ธฐ๋ณธ์ ์ผ๋ก CRUD ์์
์ ํ ๋ JPA ๊ธฐ๋ณธ API๋ฅผ ์ฌ์ฉํ๊ฑฐ๋ QL์ ์ด์ฉํ์ฌ ์ํํ๋ค.
๊ทธ๋ฌ๋ ํน์ DBMS์์ ์ ๊ณตํ๋ ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์๋๋ก ํ๊ธฐ ์ํด Native SQL ์ฌ์ฉ์ ์ง์ํ๋ค.
entityManager.createNativeQuery() ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ Native SQL์ ์คํํ ์ ์๋ค.
JPA SPEC
StringBuffer qlBuf = new StringBuffer();
qlBuf.append("SELECT * ");
qlBuf.append("FROM DEPARTMENT ");
qlBuf.append("WHERE DEPT_NAME like :condition ");
qlBuf.append("ORDER BY DEPT_NAME");
Query query = em.createNativeQuery(qlBuf.toString(),Department.class);
query.setParameter("condition", "%%");
List deptList = query.getResultList();
์์ ๊ฐ์ด ์ ์๋ SQL๋ฌธ์ ํตํด ์กฐํ ์กฐ๊ฑด์ ๋ง๋ Department ๊ฐ์ฒด์ List๊ฐ ๋ฆฌํด๋๋ค.
WHERE์ ์์ ':'์ ์ฌ์ฉํ์ฌ Named Paramenter๋ฅผ ํตํด ์กฐํ ์กฐ๊ฑด์ ์์ฑํ ์ ์๋ค.
์กฐํ ์กฐ๊ฑด์ ๊ฐ์ Query์ setParameter() ๋ฉ์๋๋ฅผ ํตํด ์ง์ ํด ์ฃผ๊ณ ์๋ค.
๋ํ, createNativeQuery์ ๋๋ฒ์ฌ ์ธ์๋ก ๋ฆฌํด๋ฐ๊ณ ์ํ๋ Entity ํด๋์ค(Department.class)๋ฅผ ์ง์ ํ ๊ฒ์ ํ์ธํ ์ ์๋ค.
์๋ํ์ธ ์ฝ๋
Query query = entityManager.createNativeQuery("SELECT * FROM Music WHERE artist = :artist", Music.class);
Music music = (Music) query.setParameter("artist", "Sunyong").getSingleResult();
System.out.println(music);
- ์บ์ ํ์ฉ ์ ์ฑ
- DB ์์ธ์ค๋ฅผ ์ต์ํ ํ๊ธฐ ์ํ ์บ์ ๊ธฐ๋ฅ ์ง์
- persist / find ๋ฑ์ ๋ํด ๋ด๋ถ์ ์ผ๋ก ์ง์
- ์ด๋ ธํ ์ด์ ์ ๋ณด๋ฅผ ์บ์ฑํ์ฌ Entity ๊ตฌ์กฐ๋ถ์ ์๋ ํฅ์
- [Get Entity by ID](Get Entity by ID)
- Method
javax.persistence.EntityManager
T find(
Class entityClass,
Object primaryKey
)
Find by primary key. Search for an entity of the specified class and primary key.
If the entity instance is contained in the persistence context, it is returned from there.
Employee employee = em.find(Employee.class, 1);
Employee managed = employee.getManager();
์๋ํ์ธ ์ฝ๋
music = entityManager.find(Music.class, new Long(19));
System.out.println(music);
- [Android DB ํ์ธํ๊ธฐ](Android DB ํ์ธํ๊ธฐ)