[英]Creating entries in a database using JPA and auto-generated primary keys
I am a Java EE/JPA newbie, currently attending a beginners course on Java EE programming. 我是Java EE / JPA新手,目前正在参加有关Java EE编程的初学者课程。
I am trying to create new entries in a database which has a sequence defined for generating an ID, which is to be used as the primary key. 我试图在数据库中创建新条目,该数据库具有定义用于生成ID的序列,该ID将用作主键。
This is the Entity, which was generated by Eclipse from the database table: 这是Entity,它是Eclipse从数据库表生成的:
@Entity
@Table(name = "ADDR_PROJ")
public class AddrProj implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "ADDR_PROJ_ID_GENERATOR", sequenceName = "ADDR_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator =
"ADDR_PROJ_ID_GENERATOR")
@Column(insertable = false, updatable = false)
private long id;
// ...
// other fields
}
(BTW, long getId()
is implemented, but void setId(long)
is not.) (顺便说一句,实现了
long getId()
,但void setId(long)
实现void setId(long)
。)
The table and sequence were created using the following SQL statements: 使用以下SQL语句创建表和序列:
CREATE TABLE addr_proj (
id INTEGER PRIMARY KEY,
name VARCHAR(30),
address VARCHAR(30),
birthday DATE,
email VARCHAR(50),
workphone DECIMAL(4,0)
);
CREATE SEQUENCE addr_seq
START WITH 1
INCREMENT BY 1
NOMAXVALUE;
Manual insertions into the table work fine using statements like this: 使用如下语句可以将表手动插入:
INSERT INTO addr_proj VALUES
(
addr_seq.nextval,
'Matt',
'3 Fake Ave, Madeuptown',
'01-apr-1980',
'matt@madeupemail.com',
'1234'
);
However, when I try to update via the DAO, I get errors like the following: 但是,当我尝试通过DAO更新时,出现如下错误:
Caused By: org.apache.openjpa.lib.jdbc.ReportingSQLException:
ORA-01400: cannot insert NULL into ("AJP05"."ADDR_PROJ"."ID")
{prepstmnt 28 INSERT INTO ADDR_PROJ (address, birthday, email, name,
workphone) VALUES (?, ?, ?, ?, ?) [params=(String) 43 Fake Ln, NotA
City, (Date) 1963-02-04, (String) pete@email.not, (String) Pete,
(BigDecimal) 1337]} [code=1400, state=23000]
at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecorator.java:192)
at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.access$700(LoggingConnectionDecorator.java:57)
at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator$LoggingConnection$LoggingPreparedStatement.executeUpdate(LoggingConnectionDecorator.java:866)
at org.apache.openjpa.lib.jdbc.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:269)
at org.apache.openjpa.jdbc.kernel.JDBCStoreManager$CancelPreparedStatement.executeUpdate(JDBCStoreManager.java:1421)
Truncated. see log file for complete stacktrace
Note that the code is trying to insert 5 values, when 6 are required - the ID is missing. 请注意,当需要6个值(缺少ID)时,代码尝试插入5个值。
The DAO code looks (something) like this: DAO代码看起来像这样:
void add(AddrProj item) {
em.persist(item);
}
What's causing this problem? 是什么引起了这个问题?
Do I need a setId(long)
method in my Entity class? 我的Entity类中需要
setId(long)
方法吗?
Do I need special code in the DAO's add()
method to manage the sequence generator? 我是否需要DAO的
add()
方法中的特殊代码来管理序列生成器?
Am I missing something else? 我还有其他东西吗?
Note: This is homework, but my choice to try using an auto-generated primary key was a bit beyond the scope of the assignment. 注意:这是家庭作业,但是我尝试使用自动生成的主键的选择超出了分配范围。 It hasn't been covered in the course.
本课程未涵盖。
Remove @Column(insertable = false, updatable = false) and the column will be part of the insert and update. 删除@Column(insertable = false,可更新= false),该列将成为插入和更新的一部分。
Check the jpa hibernate annotation documentation section "2.2.2.3. Declaring column attributes". 查看jpa hibernate注释文档部分“ 2.2.2.3。声明列属性”。
http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/ http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/
insertable (optional): whether or not the column will be part of the insert statement (default true)
updatable (optional): whether or not the column will be part of the update statement (default true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.