Hibernate Schema生成错误

时间:2014-02-03 14:58:02

标签: java sql hibernate jboss

我有一些实体'Group',如下所示(为了便于阅读,删除了getter和setter)

@Entity
public class Group {

    @Id
    @GeneratedValue
    private long id;

    @NotNull
    @Size(min = 2, max = 50, message = "Pflichtfeld")
    @Pattern(regexp = Validation.REG_NAMES, message = Validation.MSG_NAME)
    private String groupname;

    @NotNull
    @Size(min = 2, max = 50, message = "Pflichtfeld")
    private String groupDescription;

    @ManyToMany
    private List<User> members;

    @ManyToMany
    private List<Rights> ownedRights;
}

部署到我的JBoss时出现以下错误:

  

15:08:53,464 ERROR [org.hibernate.tool.hbm2ddl.SchemaUpdate](MSC服务主题1-2)HHH000388:不成功:创建表组(id bigint not null,groupDescription varchar (50)not null,groupname varchar(50)not null,primary key(id))   15:08:53,466 ERROR [org.hibernate.tool.hbm2ddl.SchemaUpdate](MSC服务线程1-2) SQL语句中的语法错误“CREATE TABLE GROUP [*](ID BIGINT NOT NULL, GROUPDESCRIPTION VARCHAR(50)NOT NULL,GROUPNAME VARCHAR(50)NOT NULL,PRIMARY KEY(ID))“; 预期“标识符”; SQL语句:   create table Group(id bigint not null,groupDescription varchar(50)not null,groupname varchar(50)not null,primary key(id))[42001-161]

My Persistence.xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
 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">
   <persistence-unit name="primary">
      <jta-data-source>java:jboss/datasources/crmDS</jta-data-source>
      <properties>
          <property name="hibernate.hbm2ddl.auto" value="update" />
          <property name="hibernate.show_sql" value="true" />
      </properties>
    </persistence-unit>
</persistence>

为什么hibernate生成的SQL有问题?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

h2数据库使用GROUP作为保留字,只要您使用它,就会在表生成和hibernate生成的其他查询中出错。

重命名您的模型类或使用the JBoss hibernate reference (esp. Section 2.1.1.1)

中记录的类级别注释@Table

使用示例:

@Entity
@Table(name="group_table")
public class Group {