自定义注解实现Hibernate的不获取调用 [英] Custom Annotation implementation Hibernate not getting called

查看:169
本文介绍了自定义注解实现Hibernate的不获取调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用自定义批注的实现,但我的实现是没有得到调用。

I need to invoke custom annotation implementation , But my implementation is not getting called. In the

低于code段,我有两个字段(ID,内容)一个配置文件对象。内容字段接受
一个字符串,

below code snippet ,I have a Profile Object with two fields (id , content). Content field accept a string and

需要通过自定义注解在运行时改变的内容。

need to change the content at runtime via a custom annotation.

我的域对象

@Entity
@Table(name = "profile", catalog = "db")
public class Profile implements java.io.Serializable{

private Integer profileId;
@ProcessContent(convertor = ProcessContent.class)
private String  content;

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "PROFILE_ID", unique = true, nullable = false)

public Integer getProfileId() {
    return profileId;
}
public void setProfileId(Integer profileId) {
    this.profileId = profileId;
}

@Column(name = "CONTENT", unique = true, nullable = false, length = 255)
public String getContent() {
    return content;
}
public void setContent(String content) {
    this.content = content;
}

}

我的自定义注解

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Target({ ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface ProcessContent {

     @SuppressWarnings("rawtypes")
        Class<? extends Object> convertor() default DefaultFieldValueConvertor.class;
}

样品标注的实现。 (请注意,这是一个简单的和复杂的逻辑来这里)

Sample Annotation implementation. (Please note that this is a sample and complex logic comes here)

public class DefaultFieldValueConvertor {

    public Object convert(Object value) {
        return (value + "Processed");
    }


}

测试

Session session = HibernateUtil.getSessionFactory().openSession();

        session.beginTransaction();
        Profile profile = new Profile();
        profile.setContent("OOOOOOOOOPSSSSSSSSS");
        session.save(profile);
        session.getTransaction().commit();

        session.close();
    }

问题 - >我可以看到经过串DB得到保存,而不是一个通过我的批注处理执行

Question - > I can see the passing string getting save in DB instead the one processed via my annotation implementation.

推荐答案

为了在JPA生命周期事件,如加载执行code,合并,提神等,你可以使用JPA的生命周期监听。你可以定义你的实体内或在自己的类侦听器回调方法。如果你在一个单一的实体类型使用回调,监听方法是最简单的方式。当你需要在不同的实体类型进行某种类型的操作使用监听器类。

In order to execute code on JPA lifecycle events like loading, merging, refreshing etc, you can use JPA lifecycle listeners. You can define listener callback methods inside your entity or in an own class. If you use the callback in a single entity type, listener methods is the easy way. Use listener classes when you need a certain type of operation to be performed on different entity types.

如果你想收藏起来之前,操纵数据,你可以结合的 @ preUpdate 和的 @ prePersist 回调。

If you want to manipulate data before storing it, you can combine the @PreUpdate and the @PrePersist callbacks.

@PreUpdate
@PrePersist
public void convert() {
   content += "Processed";
}

这篇关于自定义注解实现Hibernate的不获取调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆