2012-08-11 67 views
0

我試圖在數據庫中插入一條記錄(使用Java EE 6,EJB 3.1,JPA 2.0)。我收到一個錯誤,accountTypeId字段爲空,但我已將其設置爲自動生成。任何人都可以請建議我做錯了什麼?Java EE 6,EJB 3.1,JPA 2.0 - 在數據庫中插入記錄時出錯

以下是創建表查詢:

create table example.account_type(
    account_type_id INT NOT null PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), 
    account_type_desc varchar(20) 
); 

以下是實體類:

編輯:更新由沒有工作的NetBeans產生的實體類。我還添加了@GeneratedValue註釋,但仍然無效。

@Entity 
@Table(name = "ACCOUNT_TYPE") 
@NamedQueries({ 
    @NamedQuery(name = "AccountType.findAll", query = "SELECT a FROM AccountType a"), 
    @NamedQuery(name = "AccountType.findByAccountTypeId", query = "SELECT a FROM AccountType a WHERE a.accountTypeId = :accountTypeId"), 
    @NamedQuery(name = "AccountType.findByAccountTypeDesc", query = "SELECT a FROM AccountType a WHERE a.accountTypeDesc = :accountTypeDesc")}) 
public class AccountType implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) // ADDED THIS LINE 
    @Basic(optional = false) 
    @NotNull 
    @Column(name = "ACCOUNT_TYPE_ID") 
    private Integer accountTypeId; 
    @Size(max = 50) 
    @Column(name = "ACCOUNT_TYPE_DESC") 
    private String accountTypeDesc; 

    public AccountType() { 
    } 

    public AccountType(Integer accountTypeId) { 
     this.accountTypeId = accountTypeId; 
    } 

    public Integer getAccountTypeId() { 
     return accountTypeId; 
    } 

    public void setAccountTypeId(Integer accountTypeId) { 
     this.accountTypeId = accountTypeId; 
    } 

    public String getAccountTypeDesc() { 
     return accountTypeDesc; 
    } 

    public void setAccountTypeDesc(String accountTypeDesc) { 
     this.accountTypeDesc = accountTypeDesc; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 0; 
     hash += (accountTypeId != null ? accountTypeId.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object object) { 
     // TODO: Warning - this method won't work in the case the id fields are not set 
     if (!(object instanceof AccountType)) { 
      return false; 
     } 
     AccountType other = (AccountType) object; 
     if ((this.accountTypeId == null && other.accountTypeId != null) || (this.accountTypeId != null && !this.accountTypeId.equals(other.accountTypeId))) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return "Entities.AccountType[ accountTypeId=" + accountTypeId + " ]"; 
    } 

} 

以下是會話bean接口:

@Remote 
public interface AccountTypeSessionBeanRemote { 
    public void createAccountType(); 
    public void createAccountType(String accDesc); 
} 

以下是會話bean的實現類:

@Stateless 
public class AccountTypeSessionBean implements AccountTypeSessionBeanRemote { 
    @PersistenceContext(unitName="ExamplePU") 
    private EntityManager em; 

    @Override 
    public void createAccountType(String accDesc) { 
     AccountType emp = new AccountType(accDsc); 
     try { 
      this.em.persist(emp); 
      System.out.println("after persist"); 
     } catch(Exception ex) { 
      System.out.println("ex: " + ex.getMessage()); 
     } 
    } 
} 

下面是主要類:

public class Main { 
    @EJB 
    private static AccountTypeSessionBeanRemote accountTypeSessionBean; 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     accountTypeSessionBean.createAccountType("test"); 
    } 
} 

繼是錯誤:

INFO: ex: Object: Entities.AccountType[ accountTypeId=null ] is not a known entity type. 

回答

0

我改變了我的create table query如下:

create table example.account_type(
    account_type_id INT NOT null PRIMARY KEY, 
    account_type_desc varchar(20) 
); 

只好再添加以下行到實體類(Netbeans的不添加):

@GeneratedValue(strategy = GenerationType.AUTO) 
2

您沒有收到錯誤,因爲「accountTypeId字段爲空」。如錯誤消息所示,發生此錯誤是因爲「Entities.AccountType [accountTypeId = null]不是已知的實體類型」。

最可能的原因是AccountType沒有用@Entity註釋。通過添加它可能解決此問題。此外,它是有意義的使用

@GeneratedValue(strategy = GenerationType.IDENTITY) 

而不是自動。自動意味着提供者根據目標數據庫的功能選擇策略。根據表格定義,似乎很清楚,首選策略是IDENTITY。

+0

缺少實體註釋可能只是被剪切和粘貼,但是,如果不是,它應該在那裏。但我認爲你的第二個答案GenerationType.IDENTITY可能是根本原因。 – BillR 2012-08-12 06:14:13

+0

謝謝大家的回覆。缺少的@Entity註釋只是一個複製和粘貼錯誤。我試圖將生成類型更改爲標識,但這不起作用。我仍然得到同樣的錯誤。 – user427969 2012-08-17 00:31:29

相關問題