Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
106 views
in Technique[技术] by (71.8m points)

java - EclipseLink DataModifyQuery does not throw fatal exception

I am using SQLServer and I have an entity "Enrollment" that contains student (Student) as the ManyToOne relationship. I've tried to set the student column name as "USER" which is a SQL reserved keywords.

I've also set eclipselink.ddl-generation=drop-and-create-tables which will drop and create the table.

NOTE: I do know the reserved SQL keyword can be escaped with [USER] or "USER"

@ManyToOne
@JoinColumn(name = "USER")
private Student student;

When I execute the test main code, I do get a DatabaseException from EclipseLink and the table did not get created. However, the code successfully executed without throwing any fatal Exception that stops the execution.

It did throw Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'USER'. but I expect it to fail instead.

Is this a bug on EclipseLink itself or are there any configuration to make it fail?

Refer to the full logs below:

[EL Warning]: 2021-01-27 14:54:12.789--ServerSession(231311211)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.8.v20201217-ecdf3c32c4): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'USER'.
Error Code: 156
Call: CREATE TABLE ENROLLMENT (ENROLLMENT_KEY NUMERIC(19) NOT NULL, COURSE_KEY NUMERIC(19) NULL, USER NUMERIC(19) NULL, PRIMARY KEY (ENROLLMENT_KEY))
Query: DataModifyQuery(sql="CREATE TABLE ENROLLMENT (ENROLLMENT_KEY NUMERIC(19) NOT NULL, COURSE_KEY NUMERIC(19) NULL, USER NUMERIC(19) NULL, PRIMARY KEY (ENROLLMENT_KEY))")
[EL Warning]: 2021-01-27 14:54:12.797--ServerSession(231311211)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.8.v20201217-ecdf3c32c4): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot find the object "ENROLLMENT" because it does not exist or you do not have permissions.
Error Code: 4902
Call: ALTER TABLE ENROLLMENT ADD CONSTRAINT ENROLLMENT_COURSE_KEY FOREIGN KEY (COURSE_KEY) REFERENCES COURSE (COURSE_KEY)
Query: DataModifyQuery(sql="ALTER TABLE ENROLLMENT ADD CONSTRAINT ENROLLMENT_COURSE_KEY FOREIGN KEY (COURSE_KEY) REFERENCES COURSE (COURSE_KEY)")
[EL Warning]: 2021-01-27 14:54:12.802--ServerSession(231311211)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.8.v20201217-ecdf3c32c4): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'STUDENT_SEQ'.
Error Code: 208
Call: SELECT NEXT VALUE FOR STUDENT_SEQ
Query: ValueReadQuery(sql="SELECT NEXT VALUE FOR STUDENT_SEQ")
[EL Warning]: 2021-01-27 14:54:12.806--ServerSession(231311211)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.8.v20201217-ecdf3c32c4): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'COURSE_SEQ'.
Error Code: 208
Call: SELECT NEXT VALUE FOR COURSE_SEQ
Query: ValueReadQuery(sql="SELECT NEXT VALUE FOR COURSE_SEQ")
[EL Warning]: 2021-01-27 14:54:12.81--ServerSession(231311211)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.8.v20201217-ecdf3c32c4): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'ENROLLMENT_SEQ'.
Error Code: 208
Call: SELECT NEXT VALUE FOR ENROLLMENT_SEQ
Query: ValueReadQuery(sql="SELECT NEXT VALUE FOR ENROLLMENT_SEQ")
question from:https://stackoverflow.com/questions/65914877/eclipselink-datamodifyquery-does-not-throw-fatal-exception

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Table creation is a nice feature, but statements can have issues for any number of valid reasons that aren't failures, and EclipseLink can't, or really even try, to tell the difference.

If you want to have it fail if the tables/fields are not there when it starts up, try adding a session customizer to add validation as described here: https://wiki.eclipse.org/EclipseLink/Examples/JPA/IntegrityChecker I don't remember if the get in the example code works, as my local code using validation creates a new instance of org.eclipse.persistence.exceptions.IntegrityChecker that I add to the session. This will check each entity and verify the columns in the mappings exist, and give output detailing what might be missing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...