自定义注释实现 Hibernate 没有被调用 [英] Custom Annotation implementation Hibernate not getting called

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

问题描述

我需要调用自定义注释实现,但我的实现没有被调用.在

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

下面的代码片段,我有一个包含两个字段(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();
    }

问题 -> 我可以看到传递的字符串保存在数据库中,而不是通过我的注释实现处理的字符串.

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

推荐答案

为了在 JPA 生命周期事件(如加载、合并、刷新等)上执行代码,您可以使用 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天全站免登陆