Spring AOP アドバイスから JPA エンティティを永続化する方法はありますか? 私がやろうとするたびに、エラーjava.lang.NoSuchMethodErrorが発生します。私のpersistence.xml構成が実行時にJPAインフラストラクチャをデプロイするために、それが発生した可能性があると思います。
春の構成:
<aop:aspect ref="attachmentCreatorAdvice">
<aop:pointcut expression="execution(* com.mycomp.core.eventhandlers.*.handle*CreatedEvent(..) )" id="attachmentCreatorPointcut"/>
<aop:after-returning method="create"
pointcut-ref="attachmentCreatorPointcut" />
</aop:aspect>
AttachmentCreatorAdvice コード:
private AttachmentDao attachmentDao;
public AttachmentDao getAttachmentDao() {
return attachmentDao;
}
public void setAttachmentDao(AttachmentDao attachmentDao) {
this.attachmentDao = attachmentDao;
}
public void create(JoinPoint jp) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
Event event = (Event) jp.getArgs()[0];
Field f = event.getClass().getDeclaredField("attachments");
if ( f != null ) {
f.setAccessible(true);
List<Attachment> attachments = (List<Attachment>) f.get(event);
Field id = event.getClass().getDeclaredField("id");
if ( id != null) {
id.setAccessible(true);
AggregateIdentifier uuid = (AggregateIdentifier) id.get(event);
for( Attachment attachment : attachments ) {
AttachmentDto newAttachment = new AttachmentDto();
newAttachment.setId(UUID.randomUUID().toString());
newAttachment.setElementId(uuid.asString());
newAttachment.setDescription(attachment.getDescription());
newAttachment.setFilename(attachment.getFilename());
newAttachment.setFilesize(attachment.getFilesize());
newAttachment.setFiletype(attachment.getFiletype());
newAttachment.setLink(attachment.getLink());
newAttachment.setAttachmentBlob(DecodeUtils.fromBase64(attachment.getAttachmentBlob()));
attachmentDao.save(newAttachment);
}
}
}
}
attachmentDao は Spring コンテナから注入されます。AttachmentDto は AbstractDto から拡張されています。
@MappedSuperclass
public abstract class AbstractDto<T> implements Serializable {
@Override
public boolean equals(Object obj) {
boolean equals = false;
if ( obj instanceof AbstractDto ) {
AbstractDto o = (AbstractDto) obj;
if ( o.getId().equals( this.id ) ) {
equals = true;
}
} else {
equals = false;
}
return equals;
}
@Id
private T id;
public T getId() {
return id;
}
public void setId(T id) {
this.id = id;
}
}
attachmentDao.save メソッドが呼び出されると、次のエラーが発生します。
[EL Severe]: ejb: 2012-12-17 16:12:17.579--ServerSession(30266940)--Thread(Thread[main,5,main])--java.lang.NoSuchMethodError com.mycomp.core.data.dto.AbstractDto <init>
それが役立つことを願っています。
乾杯