2014-09-02 84 views
0

我有一個非常簡單的用戶實體,它填充(h2)數據庫中的用戶名和密碼字段。Dropwizard休眠字段不填充

我似乎無法添加更多的字段,雖然爲boolean isAdmin - 我試過原始和對象布爾值。 SQL休眠執行會被記錄下來,當我運行它時,會返回給定用戶的用戶表中的所有字段,包括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; 
    } 
} 

回答

0

其中一個是純OOP的方法是使用一個完整的構造函數(沒有ID)現在

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

,可以初始化現場將其保存到數據庫之前。

+0

該字段值位於數據庫中,它正在讀取它未填充的值。 – rich 2014-09-02 11:20:18

+0

它應該由此構造函數填充。 – 2014-09-02 11:23:19

0

明白了。

我使用的dropwizard身份驗證代碼是創建一個User對象本身,而不是使用hibernate檢索的對象。休眠代碼很好。