我开发了一些单元测试但是在所有SQL语句上都失败了,你能看一下这个日志和配置吗?
2012年7月1日下午4:33:05 org.glassfish.persistence.common.Java2DBProcessorHelper executeDDLs 警告:PER01000:执行SQLException语句" 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,KILOMETERS 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:语法错误:遇到"年份"在 第1行,第263行。2012年7月1日下午4:33:05 org.glassfish.persistence.common.Java2DBProcessorHelper executeDDLs 警告:PER01000:执行SQLException语句" CREATE TABLE 车辆(ID BIGINT非空,使变量(255)非空,模型 VARCHAR(255)NOT NULL,TRIM VARCHAR(255)\ NOT NULL,YEAR INTEGER NOT NULL,PRIMARY KEY(ID))":java.sql.SQLSyntaxErrorException:语法 错误:遇到" TRIM"在第1行第100栏。2012年7月1日4:33:06 PM org.glassfish.persistence.common.Java2DBProcessorHelper executeDDLs 警告:PER01000:执行SQLException语句" ALTER TABLE CAR ADD CONSTRAINT FK_CAR_SECURITY_ID FOREIGN KEY(SECURITY_ID) REFERENCES SECURITY(ID)":java.sql.SQLSy \ ntaxErrorException:' ALTER 表'不能在' CAR'因为它不存在7月01日, 2012年4:33:06 PM org.glassfish.persistence.common.Java2DBProcessorHelper executeDDLs 警告:PER01000:执行SQLException语句" ALTER TABLE CAR添加约束CAR_EQUIPMENT_ID外键(EQUIPMENT_ID) REFERENCES EQUIPMENT(ID)":java.sql.SQLSy \ ntaxErrorException:' ALTER 表'不能在' CAR'因为它不存在7月01日, 2012 4:33:06 PM com.sun.ejb.containers.BaseContainer initializeHome 信息:EJB5181:EJB VehicleEJB的可移植JNDI名称: 【JAVA:全球/班/ VehicleEJB com.thevehiclelist.VehicleEJB, java:global / classes / VehicleEJB] 2012年7月1日下午4:33:06 com.sun.ejb.containers.BaseContainer initializeHome INFO: EJB5181:EJB BookEJB的可移植JNDI名称: 【JAVA:全球/班/ BookEJB se.while_se.BookEJB, java:global / classes / BookEJB] 2012年7月1日下午4:33:06 com.sun.ejb.containers.BaseContainer initializeHome INFO: EJB5181:EJB CarEJB的可移植JNDI名称: 【JAVA:全球/班/ CarEJB, java:global / classes / CarEJB!se.while_se.CarEJB] Jul 01,2012 4:33:07 PM org.eclipse.persistence.session.file:/ tmp目录/ gfembed468260415984552053tmp /应用/班/ _carcmsPU 警告:本地异常堆栈:异常[EclipseLink-4002](Eclipse 持久性服务 - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException内部 例外:java.sql.SQLSyntaxErrorException:语法错误:遇到 " TRIM"在第1行第39栏。错误代码:20000呼叫:INSERT INTO 车辆(ID,MAKE,MODEL,TRIM,YEAR)价值(?,?,?,?,?) bind => [5个参数绑定]查询:InsertObjectQuery(Vehicle [id = 1,year = 2007,make = Volvo,model = V70,trim = 2.4D]) at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333) 在org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:644) 在org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:535) at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:1717) 在org.eclipse.persistence.sessions.server.ClientSession.executeCall(ClientSession.java:253) 在org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:207) 在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) 在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) 在org.eclipse.persistence.queries.InsertObjectQuery.executeCommitWithChangeSet(InsertObjectQuery.java:90) 在org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.java:287) at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:58) 在org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:844) 在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) 在org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2871)
我的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>
我的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>
我不知道它可能是什么,已经尝试了谷歌搜索很多,但没有找到任何参考代码这个东西..你能看到我的配置有什么错误或建议我的一些文件?
Best regars
答案 0 :(得分:2)
第一个错误是您尝试创建名为YEAR的列,但YEAR是Derby中的保留字。您应该选择另一个列名称。第二个是您尝试创建列TRIM,也是保留字。这里还选择另一个列名。
从我可以看到,剩余的错误是由创建第一个表时的失败引起的。
首先手动创建表并使用IDE创建相应的持久性类可能更容易且更不容易出错(Netbeans使这非常容易,我认为其他IDE具有类似的功能)。
还有一件事:为了避免自动创建255个字符宽的varchar列,您可以在注释中添加所需的长度。在那里,您还可以修复数据库列名称。例如,在db中使用名为interiortrim的32个字符宽的非空列但在代码中修剪:
@Column(name = "INTERIORTRIM", nullable = false, length=32)
private String trim;