Hibernate仅更新选定的列而不是所有已修改的字段

时间:2016-06-29 11:42:40

标签: java hibernate ejb

我有一个实体:

@Entity
@Table( name = "my_dto" )
public class MyDto implements Serializable
{
    @Id
    @GeneratedValue( generator = "uuid" )
    @GenericGenerator( name = "uuid", strategy = "uuid" )
    private String id;
    private String displayName;
    private String remark;
    @Column( name = "BACKGROUND" )
    private Integer backgroundColorInt;
    @Column( name = "FOREGROUND" )
    private Integer foregroundColorInt;

    private boolean isBold;
    private boolean isItalic;
    private boolean isUnderlined;
    @Column( name = "ISBLINKINGACTIVE" )
    private Boolean blinkingActive;
    @Column( name = "ISBLINKINGBOLD" )
    private Boolean blinkingBold;
    @Column( name = "ISBLINKINGITALIC" )
    private Boolean blinkingItalic;
    @Column( name = "ISBLINKINGUNDERLINED" )
    private Boolean blinkingUnderlined;
    @Column( name = "BLINKINGBACKGROUND" )
    private Integer blinkingBackColorInt;
    @Column( name = "BLINKINGFOREGROUND" )
    private Integer blinkingForeColorInt;

    @Column
    private String createdBy;
    @Column
    private Date createDat;
    @Column
    private String changedBy;
    @Column
    private Date changeDat;
    @Version
    private int version;

    @Transient
    private Map< String, Object > appearanceConfig = null;

    @PostLoad
    public void copyMyDto2AppearanceConfig()
    {
        if( appearanceConfig == null )
        {
            appearanceConfig = new HashMap< String, Object >();
        }
        appearanceConfig.put( BACKGROUND_COLOR, backgroundColorInt );
        appearanceConfig.put( FOREGROUND_COLOR, foregroundColorInt );
        appearanceConfig.put( FONT_STYLE, getFontStyle() );

        appearanceConfig.put( IS_BLINKING_ACTIVE, blinkingActive );
        HashMap< String, Object > blinkingConf = new HashMap< String, Object >();
        blinkingConf.put( FOREGROUND_COLOR, blinkingForeColorInt );
        blinkingConf.put( BACKGROUND_COLOR, blinkingBackColorInt );
        blinkingConf.put( FONT_STYLE, getBlinkingFontStyle() );
        appearanceConfig.put( BLINKING_CONFIGURATION, blinkingConf );
    }

    @PreUpdate
    public void copyAppearanceConfig2MyDto()
    {
        setForegroundColor( (Integer)appearanceConfig.get( FOREGROUND_COLOR ) );
        setBackgroundColor( (Integer)appearanceConfig.get( BACKGROUND_COLOR ) );
        setFontStyle( (FontStyle[])appearanceConfig.get( FONT_STYLE ) );
        setBlinkingActive( Boolean.TRUE.equals( appearanceConfig.get( IS_BLINKING_ACTIVE ) ) );
        Map< String, Object > blinkingConf =
        (Map< String, Object >)appearanceConfig.get( BLINKING_CONFIGURATION );
        if( blinkingConf != null )
        {
            setBlinkingFontStyle( (FontStyle[])blinkingConf.get( FONT_STYLE ) );
            setBlinkingForegroundColor( (Integer)blinkingConf.get( FOREGROUND_COLOR ) );
            setBlinkingBackgroundColor( (Integer)blinkingConf.get( BACKGROUND_COLOR ) );
        }
        else
        {
            setBlinkingFontStyle( null );
            setBlinkingForegroundColor( (Integer)null );
            setBlinkingBackgroundColor( (Integer)null );
        }
    }

    // some standard getters and setters...
}

我创建了一个无状态EJB,将其打包到EAR包中并将其部署到JBoss服务器(Wildfly-10)。我现在能够从数据库中创建/删除MyDto而没有任何问题。我担心的是更新操作。 EJB的更新实现如下:

@Stateless
@Remote( MyDtoStorageServiceIf.class )
public class MyDtoStorageService implements MyDtoStorageServiceIf
{
    @PersistenceContext( unitName = "DS_MY_DTO", name = "DS_MY_DTO" )
    protected EntityManager entityManager;
    @Override
    public MyDto update( MyDto aMyDto )
    {
        MyDto myDto = entityManager.merge( aMyDto );
        entityManager.flush();
        return myDto;
    }
    // other EJB methods, e.g. persisting new MyDto object, deleting selected MyDto from database etc.
}

当我尝试更新所选行时,结果只会在数据库中更新displayNameremark字段,即使aMyDto的其他某些字段也已被修改。可能原因在于MyDto类中的@PreUpdate和@PostLoad方法吗?由于我缺乏想法,我想请你指出这种奇怪行为的可能原因。我最有可能失踪的是什么?有什么问题?

1 个答案:

答案 0 :(得分:0)

在更新发生之前,详细信息将从appearanceConfig哈希映射复制到字段。

如果您的appearanceConfig哈希映射没有最新更改,那么该哈希映射中的旧值将覆盖更改。

在相关的说明中,为什么不删除所有成员字段并直接写入哈希映射?你可以拥有getter和setter,它们直接将数据设置为哈希映射。