【问题标题】:Java EE 6, EJB 3.1, JPA 2.0 - error in inserting record in databaseJava EE 6、EJB 3.1、JPA 2.0 - 在数据库中插入记录时出错
【发布时间】:2012-08-08 08:46:58
【问题描述】:

我正在尝试在数据库中插入一条记录(使用 Java EE 6、EJB 3.1、JPA 2.0)。我收到 accountTypeId 字段为空的错误,但我已将其设置为自动生成。谁能建议我做错了什么?

以下是建表查询:

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.

【问题讨论】:

    标签: java jpa-2.0 java-ee-6 ejb-3.1


    【解决方案1】:

    由于“accountTypeId 字段为空”,您没有收到错误消息。正如错误消息所述,发生错误是因为“Entities.AccountType[accountTypeId=null] 不是已知的实体类型”。

    最可能的原因是AccountType 没有用@Entity 注释。这个问题很可能通过添加它来解决。此外,使用

    是有意义的
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    

    而不是自动。自动意味着提供者根据目标数据库的能力选择策略。根据表定义,显然首选策略是 IDENTITY。

    【讨论】:

    • 丢失的实体注释可能只是被剪切和粘贴,但是,如果不是,它应该在那里。但我认为您的第二个答案 GenerationType.IDENTITY 可能是根本原因。
    • 感谢大家的回复。缺少的 @Entity 注释只是一个复制和粘贴错误。我试图将生成类型更改为身份,但这没有用。我仍然遇到同样的错误。
    【解决方案2】:

    我将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)
    

    【讨论】:

      猜你喜欢
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 2011-06-13
      • 2018-07-18
      • 1970-01-01
      • 2011-04-25
      相关资源
      最近更新 更多