没有填充的Dropwizard冬眠领域

时间:2014-09-02 10:56:09

标签: java sql hibernate h2 dropwizard

我有一个非常简单的用户实体,它填充了(h2)数据库中的用户名和密码字段。

我似乎无法为boolean isAdmin添加更多字段 - 我尝试过原始和对象布尔值。记录SQL hibernate执行,当我运行它时,返回给定用户的用户表中的所有字段,包括boolean值(true)。但Boolean仍为null

任何想法如何让这个字段填充?

@Entity
@Table(name = "users")
@NamedQueries({
    @NamedQuery(
        name = "eu.wansdyke.smaash.core.User.findAll",
        query = "SELECT u FROM User u"
    ),
    @NamedQuery(
        name = "eu.wansdyke.smaash.core.User.deleteByUsername",
        query = "DELETE FROM User WHERE username = :username"
    ),
    @NamedQuery(
        name = "eu.wansdyke.smaash.core.User.findByUsername",
        query = "SELECT u FROM User u WHERE u.username = :username"
    )
})
@NamedNativeQueries({
    @NamedNativeQuery(
        name = "eu.wansdyke.smaash.core.User.setPassword",
        query = "merge into users (username, password) key(username) values (:username, :password)"
    )
})
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "username", nullable = false)
    private String username;

    @Column(name = "password", nullable = false)
    private String password;

    @Column(name = "isadmin", nullable = false)
    private Boolean isAdmin;

    public User() {
    }

    public User(String username) {
        this.username = username;
    }

    public long getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public Boolean isAdmin() {
        return isAdmin;
    }
}

2 个答案:

答案 0 :(得分:0)

纯OOP的一种方法是使用完整的构造函数(没有id)

public User(String username, String password, Boolean isAdmin) {
  this.username = username;
  this.password = password;
  this.isAdmin = isAdmin;
}

现在,您可以在将字段保存到数据库之前初始化该字段。

答案 1 :(得分:0)

知道了。

我使用的dropwizard身份验证器代码本身就是创建一个User对象,而不是使用hibernate检索的对象。 hibernate代码很好。