8. 03
Basic O/R mapping
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
Import javax.persistence.GenerationType;
import javax.persistence.Column;
…
@Entity
Type Element Summary
String name The name of an entity.
@Table
Type Element Summary
String catalog (Optional) The catalog of the table.
String name (Optional) The name of the table.
String schema (Optional) The schema of the table.
UniqueConstraint[] uniqueConstraints (Optional) Unique constraints that are to be placed on the table.
• http://docs.oracle.com/javaee/5/api/javax/persistence/package-summary.html
9. 03
Basic O/R mapping
@Column
Type Element Summary
String columnDefinition (Optional) The SQL fragment that is used when generating the DDL for the column.
boolean insertable (Optional) Whether the column is included in SQL INSERT statements generated by
the persistence provider.
int length (Optional) The column length.
String name (Optional) The name of the column.
boolean nullable (Optional) Whether the database column is nullable.
int precision (Optional) The precision for a decimal (exact numeric) column.
int scale (Optional) The scale for a decimal (exact numeric) column.
String table (Optional) The name of the table that contains the column.
boolean unique (Optional) Whether the property is a unique key.
boolean updatable (Optional) Whether the column is included in SQL UPDATE statements generated by
the persistence provider.
• http://docs.oracle.com/javaee/5/api/javax/persistence/package-summary.html
14. 05
Others (Transaction)
import org.springframework.transaction.annotation.Transactional;
@Transactional
public void testSaveAndGet() {
String username = "test";
User user = new User();
user.username = username;
user.password = "";
user.koreanName = “test";
long id = userDao.save(user);
user = userDao.get(id);
assertEquals("Saved user and retrieved user should have the same username", username,
user.username);
}
… xmlns:tx="http://www.springframework.org/schema/tx" …
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
15. 05
Others (hbm2ddl.auto)
Value Explanation
create Session factory가 실행될 때에 스키마를 지우고 다시 생성합니다. 스키마를 생성한 다음
에 classpath에서 import.sql 파일이 있는지 찾아 이 파일에 등록된 쿼리문을 실행합니다.
create-drop create와 같지만 session factory가 내려갈 때 DB의 스키마를 삭제합니다.
update 시작하면서 도메인 객체 구성과 DB의 스키마를 비교해 필요한 테이블이나 칼럼이 없을
때 도메인 객체에 맞춰 DB 스키마를 변경합니다. 데이터나 스키마를 지우지는 않습니다.
validate 처음에 도메인 객체 구성과 DB 스키마가 같은지 확인만 할 뿐 DB 스키마에 전혀 손대지
않습니다. SessionFactory 시작 시 확인을 해서 문제가 있으면 예외를 토해내고 죽습니다.
• http://gyumee.egloos.com/2483659