weixin_42332083
2012-08-24 16:21
浏览 307
已采纳

Hibernate怎样使用Annotation映射视图

最近的项目中有几种用户,用户的字段差别极大,只有id、username、password、userType、name等几个相同的字段,现想做单点登录,采用视图方式,现在针对上面几个字段创建了一个普通视图,命名为View_User,没有主键。

然后用MyEclipse的反射工程生成Annotation配置的实体类,共生成两个实体类,如下:

 

ViewUser类

 

 

package com.ninemax.cul.user.entity;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

/**
 * ViewUser entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "VIEW_USER")
public class ViewUser implements java.io.Serializable {

    // Fields

    private ViewUserId id;

    // Constructors

    /** default constructor */
    public ViewUser() {
    }

    /** full constructor */
    public ViewUser(ViewUserId id) {
        this.id = id;
    }

    // Property accessors
    @EmbeddedId
    @AttributeOverrides({
            @AttributeOverride(name = "id", column = @Column(name = "ID", length = 50)),
            @AttributeOverride(name = "username", column = @Column(name = "USERNAME", length = 32)),
            @AttributeOverride(name = "password", column = @Column(name = "PASSWORD", length = 32)),
            @AttributeOverride(name = "userType", column = @Column(name = "USER_TYPE")),
            @AttributeOverride(name = "name", column = @Column(name = "NAME", length = 65536)),
            @AttributeOverride(name = "isWorkFlag", column = @Column(name = "IS_WORK_FLAG")) })
    public ViewUserId getId() {
        return this.id;
    }

    public void setId(ViewUserId id) {
        this.id = id;
    }

}

 另外一个类是ViewUserId类:

 

package com.ninemax.cul.user.entity;

import javax.persistence.Column;
import javax.persistence.Embeddable;

/**
 * ViewUserId entity. @author MyEclipse Persistence Tools
 */
@Embeddable
public class ViewUserId implements java.io.Serializable {

    // Fields

    private String id;
    private String username;
    private String password;
    private Integer userType;
    private String name;
    private Boolean isWorkFlag;

    // Constructors

    /** default constructor */
    public ViewUserId() {
    }

    /** full constructor */
    public ViewUserId(String id, String username, String password,
            Integer userType, String name, Boolean isWorkFlag) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.userType = userType;
        this.name = name;
        this.isWorkFlag = isWorkFlag;
    }

    // Property accessors

    @Column(name = "ID", length = 50)
    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Column(name = "USERNAME", length = 32)
    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Column(name = "PASSWORD", length = 32)
    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name = "USER_TYPE")
    public Integer getUserType() {
        return this.userType;
    }

    public void setUserType(Integer userType) {
        this.userType = userType;
    }

    @Column(name = "NAME", length = 65536)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "IS_WORK_FLAG")
    public Boolean getIsWorkFlag() {
        return this.isWorkFlag;
    }

    public void setIsWorkFlag(Boolean isWorkFlag) {
        this.isWorkFlag = isWorkFlag;
    }

    public boolean equals(Object other) {
        // 省略
    }

    public int hashCode() {
        // 省略
    }

}

 其实这样程序是可以运行的,问题在于视图中的name是可以为空的,在name为空时Hibernate加载不上来数据(为null),请问怎样才能让Hibernate将含有null字段值的记录也加载上来?

网上有人介绍XML配置的解决方式,但我经验不足,没有参照着将Annotation下的解决办法找到,请各位指点一下,谢谢!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • jinnianshilongnian 2012-08-24 16:27
    已采纳

    是为null的显示为“”吗? 还是?

    如果是为null的显示为“”,可以:
    用公式 @Formula(value="case username is null then '' else username end")
    删掉 @Column

    点赞 打赏 评论

相关推荐 更多相似问题