>>分享JPA、Hibernate、Mybatis对象持久化技术,对《精通JPA与Hibernate:Java对象持久化详解》提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 23812 个阅读者 刷新本主题
 * 贴子主题:  JPA @Convert注解的用法 回复文章 点赞(0)  收藏  
作者:flybird    发表时间:2020-02-17 13:50:05     消息  查看  搜索  好友  邮件  复制  引用

1. Convert注解介绍
1.1 基本用途
1.2 参考maven依赖
1.3 注解定义

2. 使用案例 2.1 背景&目标
2.2 convert实现
2.3 convert使用


1. Convert注解介绍

1.1 基本用途

    实现java bean属性或字段与数据库字段的mapping

1.2 参考maven依赖
<dependency>

    <groupId>org.hibernate.javax.persistence</groupId>

    <artifactId>hibernate-jpa-2.1-api</artifactId>

    <version>1.0.2.Final</version>

</dependency>

1.3 注解定义


@Target({METHOD, FIELD, TYPE})

@Retention(RUNTIME)

public @interface Convert {

   /**

    * Specifies the converter to be applied. A value for this

    * element must be specified if multiple converters would

    * otherwise apply.

    */


   Class converter() default void.class;

   /**

    * The attributeName must be specified unless the Convert annotation

    * is on an attribute of basic type or on an element collection of

    * basic type. In these cases, attributeName must not be

    * specified.

    */


   String attributeName() default "";

   /**

    * Used to disable an auto-apply or inherited converter.

    * If disableConversion is true, the converter element should

    * not be specified.

    */


   boolean disableConversion() default false;

}

2. 使用案例

2.1 背景&目标

背景:播单推荐时间段有很多组,对应的数据库决定使用一个json string字段存储。

目标:期望java代码中按照List<TimeDetailInfo>简洁编码,而使用jpa保存/查询时自动映射到数据库中的time_detail_info varchar(500)存储

2.2 convert实现


import com.alibaba.fastjson.JSONObject;

import com.google.common.collect.Lists;

import org.apache.commons.lang3.StringUtils;

import javax.persistence.AttributeConverter;

import java.util.List;

/**

* 播单推荐有效期时间段字段转换器

*/


public class TimeDetailInfosConvert implements AttributeConverter<List<TimeDetailInfo>, String> {



    /**

     * jave bean属性/字段映射到数据库字段

     * @param attribute

     * @return

     */


    @Override

    public String convertToDatabaseColumn(List<TimeDetailInfo> attribute) {

        if (attribute == null)

            return StringUtils.EMPTY;

        return JSONObject.toJSONString(attribute);

    }



    /**

     * 数据库字段映射到java bean属性/字段

     * @param dbData

     * @return

     */


    @Override

    public List<TimeDetailInfo> convertToEntityAttribute(String dbData) {

        if (StringUtils.isBlank(dbData))

            return Lists.newArrayList();

        return JSONObject.parseArray(dbData, TimeDetailInfo.class);

    }

}

2.3 convert使用


import lombok.Data;

import javax.persistence.*;

import java.util.List;

/**

* 播单推荐配置

*/


@Data

@Entity

public class PlaylistRecommendConfig extends BasicEntity{



    // 推荐有效时间段

    @Convert(converter = TimeDetailInfosConvert.class)

    @Column(name="time_detail_info",columnDefinition="varchar(500)")

    private List<TimeDetailInfo> timeDetailInfos;

}

程序猿的技术大观园:www.javathinker.net

[这个贴子最后由 flybird 在 2020-02-19 12:37:14 重新编辑]
网站系统异常


系统异常信息
Request URL: http://www.javathinker.net/WEB-INF/lybbs/jsp/topic.jsp?postID=761

java.lang.NullPointerException

如果你不知道错误发生的原因,请把上面完整的信息提交给本站管理人员