하이버네이트를 사용하다보면 객체를 저장할 때 생성 필드에 값이 자동으로 들어 갔으면 하는 때를 느낄 때가 있다. 예를 들면 createdAt이나 updatedAt 같은 경우이다.
이럴 경우 하이버네이트에서 객체를 가로채서 처리하는 기능은 interceptor를 사용하면 된다. 방법은 Interceptor 인터페이스를 implements 하거나 EmptyInterceptor 클래스를
상속받는 것이다. 다음의 그 예제이다.
- public class DateInterceptor extends EmptyInterceptor {
private static final long serialVersionUID = 1L;
@Override
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) {
Date currentTimestamp = null; - boolean changed = false;
for (int i = 0; i < state.length; i++) {
if ("createdAt".equals(propertyNames[i])) {
state[i] = new Date();
changed = true;
}
if ("updatedAt".equals(propertyNames[i])) {
state[i] = new Date();
changed = true;
}
}
return changed;
}
@Override
public boolean onFlushDirty(Object entity, Serializable id,
Object[] currentState, Object[] previousState,
String[] propertyNames, Type[] types) {
boolean changed = false;
for (int i = 0; i < currentState.length; i++) {
if ("updatedAt".equals(propertyNames[i])) {
currentState[i] = new Date();
changed = true;
break;
}
}
return changed;
}
}
스프링을 사용한다면 하이버네이트 세션 팩토리에 다음을 추가하자~~
- <bean id="sessionFactory">
<property name="entityInterceptor">
<bean class="glory.board.model.hibernate.interceptor.DateInterceptor"/>
</property>
</bean>
이 글은 스프링노트에서 작성되었습니다.
OpenID Bookmarklet Generator
※openmaru lab 사이트가 없어져서 빽업 차원에서 여기에 옮겨놨습니다. 이것은 제가 만든건 아니고 mywizz에 의해서 만들어 졌습니다.
springnote,lifepod 등의 OpenID 지원 사이트에서 처럼 생긴 입력창이 보일 때, 꾸욱 누르면 오픈아이디를 자동으로 채워주고 로그인 버튼까지 눌러주는 Bookmarklet입니다.
아래의 순서에 따라 만들어 보세요~!
[New] 오픈아이디 로그인 창이 없는 페이지에서 누를 경우 지금까지는 아무 일도 일어나지 않았습니다만, 이제 자신의 오픈아이디 인증 사이트(myid.net이나 idtail.com)에서 바로 로그인할 수 있도록 수정되었습니다.
오픈아이디 로그인을 원하는 경우에는 언제라도 눌러도 되겠죠? :)
* 최초 이용 시 lab01.openmaru.com을 승인해 주셔야 합니다~
In my-spring-app-servlet.xml
- <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
">
<import resource="someContext.xml"/>
</beans>
In someContext.xml
※ 주의사항 : 아래의 xmls, xmlns:xsi, xsi:schemaLocation 등 모든 정보를 포함하고 있어야 에러가 안난다.
- <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd
">
<util:list id="solrArticleServers00">
<util:list>
<value>http://121.125.10.11:8080/article
</value>
<value>http://121.125.10.11:8070/article
</value>
</util:list>
</util:list>
<util:list id="solrArticleServers01">
<util:list>
<value>http://121.125.10.12:8080/article
</value>
<value>http://121.125.10.12:8070/article
</value>
</util:list>
</util:list>
<util:map id="articlePartitionMap" key-type="java.lang.Integer">
<entry key="0" value-ref="solrArticleServers00" />
<entry key="1" value-ref="solrArticleServers01" />
</util:map>
</beans>
이 글은 스프링노트에서 작성되었습니다.


