简体   繁体   English

Derby和Circumflex ORM的SQL语法错误

[英]SQL syntax error with Derby and Circumflex ORM

I'm trying to use Circumflex ORM (as suggested on StackOverflow - here , here and here ) to connect to a local (embedded) Apache Derby database over JDBC from a Scala project (built with simple build tool). 我正在尝试使用Circumflex ORM (在StackOverflow上建议 - 此处此处此处 )通过JDBC从Scala项目(使用简单构建工具构建)连接到本地(嵌入式)Apache Derby数据库。 I've followed the instructions carefully, but am having some interesting problems. 我仔细按照说明操作,但我遇到了一些有趣的问题。

Here's the driver and URL components of the cx.properties file: 这是cx.properties文件的驱动程序和URL组件:

orm.connection.driver=org.apache.derby.jdbc.EmbeddedDriver
orm.connection.url=jdbc:derby:derbyDB

(These map to the "instance creation of the reflected driver and create Connection" model with raw JDBC, or the equivalents in persistence.xml - Circumflex is using a short and sweet properties file because, you know, it's not XML and that's a good thing.) (这些映射到“反射驱动程序的实例创建和创建连接”模型与原始JDBC或persistence.xml中的等价物 - Circumflex使用一个简短而又甜蜜的属性文件,因为,你知道,它不是XML,这是一个很好的事情。)

The dependencies I've added in my sbt project file that are directly relevant are: 我在sbt项目文件中添加的直接相关的依赖项是:

  "ru.circumflex" % "circumflex-orm" % "1.0",
  "org.apache.derby" % "derby" % "10.6.1.0"

I have created a short example model which defines a simplified version of the table that the documentation describes: 我创建了一个简短的示例模型,它定义了文档描述的表的简化版本:

import java.sql.DriverManager
import ru.circumflex.orm._

class Country extends Record[Country] {
  val code = "code" VARCHAR(2)
  val name = "name" TEXT
}

object Country extends Table[Country]

This seems to compile okay, and I can instantiate the Country object (using a Scala 2.8.0 RC5 shell invoked with sbt console) and create an object ActiveRecord-style and then save it like this: 这似乎编译好了,我可以实例化Country对象(使用sbt控制台调用的Scala 2.8.0 RC5 shell)并创建一个ActiveRecord对象样式,然后像这样保存:

val c = new Country
c.code := "US"
c.name := "United States of America"
c.save

According to the documentation, this should run a validation over the object and then insert it into the database. 根据文档,这应该对对象运行验证,然后将其插入数据库。 I get the following exception: 我得到以下异常:

java.sql.SQLSyntaxErrorException: Syntax error: Encountered "public" at line 1, column 13.
        at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
        at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
        at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
        at org.apache.derby.impl.jdbc.EmbedPreparedStatement.<init>(Unknown Source)
        at org.apache.derby.impl.jdbc.EmbedPreparedStatement20.<init>(Unknown Source)
        at org.apache.derby.impl.jdbc.EmbedPreparedStatement30...

I found this thread where someone is having a similar problem with 'Encountered "public"' and Apache Derby, but the replies don't seem to suggest a useful way of going forward. 我找到了这个帖子 ,其中某人遇到了与'遭遇'公共“和Apache Derby类似的问题,但回复似乎没有提出一个有用的前进方式。

Any ideas what might be causing this? 可能导致这种情况的任何想法?

It's possible you need to tell Circumflex to use Derby syntax explicitly, instead of expecting it to be inferred from the JDBC driver classname and URL. 您可能需要告诉Circumflex显式使用Derby语法,而不是期望从JDBC驱动程序类名和URL推断出它。

eg in Hibernate you need to set the dialect... 例如在Hibernate中你需要设置方言......

And it seems you can work around it by setting the "orm.defaultSchema" property to something other than "public", which seems to be a reserved word in Derby. 而且似乎你可以通过将“orm.defaultSchema”属性设置为“public”之外的其他东西来解决它,这似乎是Derby中的保留字

And, final edit, most of the time people don't bother to use an explicit schema name when creating tables, and they just get the default, but Circumflex seems to always add it, so for Derby, you should be able to use "APP" as the schema name, or create your own schema beforehand and use its name. 并且,最终编辑,大多数时候人们在创建表时不打算使用显式模式名称,并且他们只是获得默认值,但Circumflex似乎总是添加它,因此对于Derby,您应该能够使用“ APP“作为模式名称,或者事先创建自己的模式并使用其名称。

I've been using Circumflex ORM with Hypersonic. 我一直在使用Circumflex ORM和Hypersonic。 It only supports Postgres, MySql and Oracle by default. 它默认只支持Postgres,MySql和Oracle。 You need to extend ru.circumflex.orm.Dialect to supply the correct SQL syntax for Derby, and declare this class in your cx.properties file. 您需要扩展ru.circumflex.orm.Dialect以为Derby提供正确的SQL语法,并在cx.properties文件中声明此类。 ie

orm.dialect=com.magmanics.circumflex.orm.dialect.HsqldbDialect

Here is my Hypersonic dialect file, it might help you if you are just looking for a simple in memory database... 这是我的Hypersonic方言文件,如果您只是在寻找一个简单的内存数据库,它可能对您有所帮助......

import ru.circumflex.orm._

 /** * @author James Baxter <jwbaxter(at)gmail> * @since 19-Jun-2010 */ class HsqldbDialect extends Dialect { override def timestampType = "TIMESTAMP" override def createSchema(schema: Schema) = "CREATE SCHEMA " + schema.name + " AUTHORIZATION DBA" override def createTable(table: Table[_]) = "CREATE TABLE " + table.qualifiedName + " (" + table.fields.map(_.toSql).mkString(", ") + ")" override def columnDefinition(field: Field[_]): String = { var result = field.name + " " + field.sqlType field.default match { case Some(expr) => result += " " + expr case _ => } if (!field.nullable_? && !result.contains("PRIMARY KEY")) result += " NOT NULL" return result } override def primaryKeyExpression(record: Record[_]) = "IDENTITY PRIMARY KEY" override def initializeRelation(relation: Relation[_]) = {} override def lastIdExpression(node: RelationNode[_]) = node.alias + "." + node.relation.primaryKey.name + " = IDENTITY()" } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Circumflex orm不适用于Play 2.1 - Circumflex orm doesn't work with Play 2.1 使用正确的servlet过滤器将circumflex-orm集成到play 2.0 scala应用程序中 - integrate circumflex-orm in play 2.0 scala application with correct servlet filter 使用scala和circumflex-orm类在play框架2中提供自己的unapply方法 - provide own unapply method in play framework 2 with scala and circumflex-orm class 带有SQL插值的ScalikeJDBC的未知SQL语法错误 - Unknown SQL syntax error for ScalikeJDBC with SQL interpolation 更新并选择单个查询。 Scala中的SQL语法错误 - Update and select in single query . Sql syntax error in Scala scalatra是否在幕后使用了抑扬音? - Does scalatra use circumflex behind the scenes? 每次执行 toDF() 或 createDataFrame 时,scala spark 都会引发与 derby 相关的错误 - scala spark raises an error related to derby everytime when doing toDF() or createDataFrame 比较提示语法错误 - Comparison prompts syntax error Lightbend示例语法错误 - Lightbend examples syntax error 在Scala REPL中运行spark时出错-访问被拒绝org.apache.derby.security.SystemPermission(“ engine”,“ usederbyinternals”) - Error running spark in a Scala REPL - access denied org.apache.derby.security.SystemPermission( “engine”, “usederbyinternals” )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM