问题
I develop some unit tests but fail on all SQL statements, can you please look at this log and configuration?
Jul 01, 2012 4:33:05 PM org.glassfish.persistence.common.Java2DBProcessorHelper executeDDLs WARNING: PER01000: Got SQLException executing statement "CREATE TABLE CAR (ID BIGINT NOT NULL, BRAND VARCHAR(255) NOT NULL, DESCRIPTION VARCHAR(255), FUEL VARCHAR(255) NOT NU\ LL, GEARBOX VARCHAR(255) NOT NULL, KILOMETRES INTEGER NOT NULL, MODEL VARCHAR(255) NOT NULL, PRICE INTEGER NOT NULL, TYPE VARCHAR(255) NOT NULL, YEAR INTEGER NOT NULL, EQUIPM\ ENT_ID BIGINT, SECURITY_ID BIGINT, PRIMARY KEY (ID))": java.sql.SQLSyntaxErrorException: Syntax error: Encountered "YEAR" at line 1, column 263. Jul 01, 2012 4:33:05 PM org.glassfish.persistence.common.Java2DBProcessorHelper executeDDLs WARNING: PER01000: Got SQLException executing statement "CREATE TABLE VEHICLE (ID BIGINT NOT NULL, MAKE VARCHAR(255) NOT NULL, MODEL VARCHAR(255) NOT NULL, TRIM VARCHAR(255) \ NOT NULL, YEAR INTEGER NOT NULL, PRIMARY KEY (ID))": java.sql.SQLSyntaxErrorException: Syntax error: Encountered "TRIM" at line 1, column 100. Jul 01, 2012 4:33:06 PM org.glassfish.persistence.common.Java2DBProcessorHelper executeDDLs WARNING: PER01000: Got SQLException executing statement "ALTER TABLE CAR ADD CONSTRAINT FK_CAR_SECURITY_ID FOREIGN KEY (SECURITY_ID) REFERENCES SECURITY (ID)": java.sql.SQLSy\ ntaxErrorException: 'ALTER TABLE' cannot be performed on 'CAR' because it does not exist. Jul 01, 2012 4:33:06 PM org.glassfish.persistence.common.Java2DBProcessorHelper executeDDLs WARNING: PER01000: Got SQLException executing statement "ALTER TABLE CAR ADD CONSTRAINT CAR_EQUIPMENT_ID FOREIGN KEY (EQUIPMENT_ID) REFERENCES EQUIPMENT (ID)": java.sql.SQLSy\ ntaxErrorException: 'ALTER TABLE' cannot be performed on 'CAR' because it does not exist. Jul 01, 2012 4:33:06 PM com.sun.ejb.containers.BaseContainer initializeHome INFO: EJB5181:Portable JNDI names for EJB VehicleEJB: [java:global/classes/VehicleEJB!com.thevehiclelist.VehicleEJB, java:global/classes/VehicleEJB] Jul 01, 2012 4:33:06 PM com.sun.ejb.containers.BaseContainer initializeHome INFO: EJB5181:Portable JNDI names for EJB BookEJB: [java:global/classes/BookEJB!se.while_se.BookEJB, java:global/classes/BookEJB] Jul 01, 2012 4:33:06 PM com.sun.ejb.containers.BaseContainer initializeHome INFO: EJB5181:Portable JNDI names for EJB CarEJB: [java:global/classes/CarEJB, java:global/classes/CarEJB!se.while_se.CarEJB] Jul 01, 2012 4:33:07 PM org.eclipse.persistence.session.file:/tmp/gfembed468260415984552053tmp/applications/classes/_carcmsPU WARNING: Local Exception Stack: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLSyntaxErrorException: Syntax error: Encountered "TRIM" at line 1, column 39. Error Code: 20000 Call: INSERT INTO VEHICLE (ID, MAKE, MODEL, TRIM, YEAR) VALUES (?, ?, ?, ?, ?) bind => [5 parameters bound] Query: InsertObjectQuery(Vehicle [id=1, year=2007, make=Volvo, model=V70, trim=2.4D]) at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333) at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:644) at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:535) at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:1717) at org.eclipse.persistence.sessions.server.ClientSession.executeCall(ClientSession.java:253) at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:207) at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:193) at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.insertObject(DatasourceCallQueryMechanism.java:342) at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:162) at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:177) at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.insertObjectForWrite(DatabaseQueryMechanism.java:472) at org.eclipse.persistence.queries.InsertObjectQuery.executeCommit(InsertObjectQuery.java:80) at org.eclipse.persistence.queries.InsertObjectQuery.executeCommitWithChangeSet(InsertObjectQuery.java:90) at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.java:287) at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:58) at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:844) at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:743) at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:108) at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:85) at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2871)
My persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="carcmsPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/__default</jta-data-source>
<class>se.while_se.Book</class>
<class>se.while_se.Car</class>
<class>com.thevehiclelist.Vehicle</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby:MyTestDb;create=true" />
<property name="javax.persistence.jdbc.user" value="" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
</properties>
</persistence-unit>
</persistence>
And my pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.8.2.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
I dont have any clue what it can be, have tryed googling around a lot, but did not found any reference code for this thing.. Can you please see whats wrong in my configuration or advice me for some documentation?
Best regars
回答1:
The very first error is that you try to create a column called YEAR, but YEAR is a reserved word in Derby. You should select another column name. The second one is that you try to create a column TRIM, also a reserved word. Here also select another column name.
From what I can see the remaining errors are caused by the failure during the creation of the first tables.
It might be easier and less error prone to first create the tables manually and the use your IDE to create the corresponding persistence class (Netbeans makes that very easy, I assume other IDEs have a comparable function).
One more thing: to avoid the automatic creation of 255 character wide varchar columns you can add the desired length in the annotation. There you can also fix the database column name. Example, with a 32 char wide not null column called interiortrim in the db but trim in code:
@Column(name = "INTERIORTRIM", nullable = false, length=32)
private String trim;
来源:https://stackoverflow.com/questions/11282889/embedded-glassfish-sql-statement-fail