Created
December 21, 2012 21:05
-
-
Save nakamura-to/4355837 to your computer and use it in GitHub Desktop.
Doma 改善案。 汎用的なDelegateで論理削除。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tutorial.dao; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Method; | |
import org.seasar.doma.jdbc.Config; | |
public class DeleteDelegate { | |
protected final Config config; | |
protected final Object dao; | |
// DaoをObject型で受ける | |
public DeleteDelegate(Config config, Object dao) { | |
this.config = config; | |
this.dao = dao; | |
} | |
// EntityをObject型で受ける | |
public int delete(Object entity) { | |
try { | |
Field f = entity.getClass().getDeclaredField("deleteFlag"); | |
f.setAccessible(true); | |
f.set(entity, true); | |
Method m = dao.getClass().getMethod("update", entity.getClass()); | |
return (Integer) m.invoke(dao, entity); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tutorial.dao; | |
import org.seasar.doma.Dao; | |
import org.seasar.doma.Delegate; | |
import org.seasar.doma.Select; | |
import org.seasar.doma.Update; | |
import tutorial.AppConfig; | |
import tutorial.entity.Employee; | |
@Dao(config = AppConfig.class) | |
public interface EmployeeDao { | |
@Select | |
Employee selectById(int id); | |
@Delegate(to = DeleteDelegate.class) | |
int delete(Employee employee); | |
@Update | |
int update(Employee employee); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment