File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Incorrect syntax near the keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Incorrect syntax near the keyword" Watch "Incorrect syntax near the keyword" New topic
Author

Incorrect syntax near the keyword

John Smith
Ranch Hand

Joined: Aug 21, 2004
Posts: 48
Hi All:

I am using Hibernate 3.2,MyEclipse 6, SQL 2005, and SQL DRIVER:jdtd-1.2
When I used hibernate to create a table called "user", I get the error:
16:30:24,484 DEBUG SchemaExport:289 - Incorrect syntax near the keyword 'user'.

This is my Hibernate.cfg.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="connection.username">sa</property>
<property name="connection.url">jdbc:jtds:sqlserver://xxx:1433/examscam</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="myeclipse.connection.profile">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.password">mychau1</property>
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
</session-factory>

</hibernate-configuration>

This is my java code:

package com.examscam.model;
import javax.persistence.Basic;import javax.persistence.Column;
import javax.persistence.Entity;import javax.persistence.GeneratedValue;
import javax.persistence.Id;import javax.persistence.Table;
import javax.persistence.Temporal;import javax.persistence.TemporalType;
import javax.persistence.Transient;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

@Entity
@Table(name = "user", schema = "examscam")
public class User {
private Long id;
private String loginName;
private String password;
private String encryptedPassword;
private String emailAddress;
private Boolean verified;
private java.util.Date lastAccessTime;
private java.util.Calendar registrationDate;

public User(){
//registrationDate = new java.util.GregorianCalendar();
//lastAccessTime = new java.util.Date();
//verified = false;
}

@Transient
public String getEncryptedPassword() {
return encryptedPassword;
}
public void setEncryptedPassword(String ep) {
this.encryptedPassword = ep;
}
@Id
@GeneratedValue
@Column(name = "id")
public Long getId() { return id; }

public void setId(Long id) { this.id = id; }

@Column(name = "login_name")
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}

@Column(name = "password", nullable=false)
public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getEmailAddress() {
return emailAddress;
}

@Temporal(TemporalType.TIMESTAMP)
public java.util.Date getLastAccessTime() {
return lastAccessTime;
}

@Temporal(TemporalType.DATE)
public java.util.Calendar getRegistrationDate() {
return registrationDate;
}

@Basic
public Boolean isVerified() {
return verified;
}

public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}

public void setLastAccessTime(java.util.Date lastAccessTime) {
this.lastAccessTime = lastAccessTime;
}

public void setRegistrationDate(java.util.Calendar registrationDate){
this.registrationDate = registrationDate;
}

public void setVerified(Boolean verified) {
this.verified = verified;
}

public String toString() {
return id + " : " + loginName + " : " + password + " : " + emailAddress;
}

public static void main(String args[])
{
AnnotationConfiguration config = new AnnotationConfiguration();
config.addAnnotatedClass(User.class);
config.configure();
new SchemaExport(config).create(true,true);
}
}

When I runned it I get the following error. I donot seem to be able to create the table called "user" in my schema examscam.
16:30:23,640 INFO Version:15 - Hibernate Annotations 3.3.0.GA
16:30:23,656 INFO Environment:514 - Hibernate 3.2.5
16:30:23,656 INFO Environment:547 - hibernate.properties not found
16:30:23,656 INFO Environment:681 - Bytecode provider name : cglib
16:30:23,671 INFO Environment:598 - using JDK 1.4 java.sql.Timestamp handling
16:30:23,734 INFO Configuration:1426 - configuring from resource: /hibernate.cfg.xml
16:30:23,781 INFO Configuration:1403 - Configuration resource: /hibernate.cfg.xml
16:30:23,984 INFO Configuration:1541 - Configured SessionFactory: null
16:30:24,000 INFO Dialect:152 - Using dialect: org.hibernate.dialect.SQLServerDialect
16:30:24,093 INFO AnnotationBinder:398 - Binding entity from annotated class: com.examscam.model.User
16:30:24,125 INFO EntityBinder:420 - Bind entity com.examscam.model.User on table user
16:30:24,187 INFO Version:17 - Hibernate Validator 3.0.0.GA
16:30:24,359 INFO SchemaExport:154 - Running hbm2ddl schema export
16:30:24,359 DEBUG SchemaExport:170 - import file not found: /import.sql
16:30:24,359 INFO SchemaExport:179 - exporting generated schema to database
16:30:24,359 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
16:30:24,359 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 20
16:30:24,359 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
16:30:24,375 INFO DriverManagerConnectionProvider:80 - using driver: net.sourceforge.jtds.jdbc.Driver at URL: jdbc:jtds:sqlserver://xxx:1433/examscam
16:30:24,375 INFO DriverManagerConnectionProvider:86 - connection properties: {user=sa, password=****}
drop table examscam.user
16:30:24,484 DEBUG SchemaExport:303 - drop table examscam.user
16:30:24,484 DEBUG SchemaExport:288 - Unsuccessful: drop table examscam.user
16:30:24,484 DEBUG SchemaExport:289 - Incorrect syntax near the keyword 'user'.
create table examscam.user (id numeric(19,0) identity not null, emailAddress varchar(255) null, lastAccessTime datetime null, login_name varchar(255) null, password varchar(255) not null, registrationDate datetime null, verified tinyint null, primary key (id))
16:30:24,484 DEBUG SchemaExport:303 - create table examscam.user (id numeric(19,0) identity not null, emailAddress varchar(255) null, lastAccessTime datetime null, login_name varchar(255) null, password varchar(255) not null, registrationDate datetime null, verified tinyint null, primary key (id))
16:30:24,484 ERROR SchemaExport:274 - Unsuccessful: create table examscam.user (id numeric(19,0) identity not null, emailAddress varchar(255) null, lastAccessTime datetime null, login_name varchar(255) null, password varchar(255) not null, registrationDate datetime null, verified tinyint null, primary key (id))
16:30:24,484 ERROR SchemaExport:275 - Incorrect syntax near the keyword 'user'.
16:30:24,484 INFO SchemaExport:196 - schema export complete
16:30:24,484 INFO DriverManagerConnectionProvider:147 - cleaning up connection pool: jdbc:jtds:sqlserver://xxx:1433/examscam


Any hint or help would be greatly appreciated.

Yours,

Frustrated
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

"user" is a keyword in most databases. Typically in SQL Server if you want to create a table using a keyword you can, but you have to escape it with square brakets. If you are using Hibernate to generate your DDL you can't do this. I'd rename your table.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
John Smith
Ranch Hand

Joined: Aug 21, 2004
Posts: 48
Hi:

You're exactly right!!!
Thank you for your help!!!
Here is the new code to make it work:
import javax.persistence.Basic;import javax.persistence.Column;
import javax.persistence.Entity;import javax.persistence.GeneratedValue;
import javax.persistence.Id;import javax.persistence.Table;
import javax.persistence.Temporal;import javax.persistence.TemporalType;
import javax.persistence.Transient;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

@Entity
@Table(name = "[user]", schema = "dbo", catalog = "examscam")
public class User
{
private Long id;
private String password;

@Id
@GeneratedValue
public Long getId()
{
return id;
}

public void setId(Long id)
{
this.id = id;
}

public String getPassword()
{
return password;
}

public void setPassword(String password)
{
this.password = password;
}
public static void main(String args[])
{
AnnotationConfiguration config = new AnnotationConfiguration();
config.addAnnotatedClass(User.class);
config.configure();
new SchemaExport(config).create(true,true);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Incorrect syntax near the keyword