I am a Java EE/JPA newbie, currently attending a beginners course on Java EE programming.
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.
This is the Entity, which was generated by Eclipse from the database table:
@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.)
The table and sequence were created using the following SQL statements:
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:
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.
The DAO code looks (something) like this:
void add(AddrProj item) {
em.persist(item);
}
What's causing this problem?
Do I need a setId(long)
method in my Entity class?
Do I need special code in the DAO's add()
method to manage the sequence generator?
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.
Check the jpa hibernate annotation documentation section "2.2.2.3. Declaring column attributes".
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.