我正在尝试在数据库中插入记录(使用Java EE 6,EJB 3.1,JPA 2.0)。我收到一个错误,accountTypeId字段为空,但我已将其设置为autogenerate。任何人都可以建议我做错了什么?
以下是创建表查询:
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());
}
}
}
以下是Main类:
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 :(得分:2)
由于“accountTypeId字段为空”,您没有收到错误。如错误消息所示,发生错误是因为“Entities.AccountType [accountTypeId = null]不是已知的实体类型”。
最可能的原因是AccountType
未注明@Entity。添加它可能会解决此问题。另外,使用
@GeneratedValue(strategy = GenerationType.IDENTITY)
而不是AUTO。 Auto表示提供程序根据目标数据库的功能选择策略。根据表格定义,似乎很清楚首选策略是IDENTITY。
答案 1 :(得分: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)