表不是由hibernate的hbm2ddl创建的

时间:2011-06-08 23:01:17

标签: java hibernate

在我的应用程序中,我有几个用JPA注释注释的实体POJO。此外,hbm2ddl配置为为这些实体生成表。当应用程序第一次启动时,除了一个表外,所有表都成功生成。 这是实体源代码:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "REQUESTS")
public class InterpreterRequest implements java.io.Serializable {

    private static final long serialVersionUID = -1017432073323298138L;

    @Id
    @GeneratedValue
    private long id;
    @Column(name = "quantity")
    private int quantity;
    @Column(name = "from")
    private String from;
    @Column(name = "to")
    private String to;
    @Column(name = "spec")
    private String spec;
    @ManyToOne(targetEntity = Event.class)
    private Event event;

    public long getId() {
        return id;
    }

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

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getSpec() {
        return spec;
    }

    public void setSpec(String spec) {
        this.spec = spec;
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

}

这是hibernate会话工厂配置:

    <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
                    <prop key="hibernate.connection.pool_size">5</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
                    <!-- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> 
                        <prop key="hibernate.hibernate.cache.use_query_cache">true</prop> -->
                </props>
            </property>
            <property name="annotatedClasses">
                <list>
                    <value>com.ceonline.inter.shared.model.User</value>
                    ...
<value>com.ceonline.inter.shared.model.InterpreterRequest</value>
                </list>
            </property>
        </bean>

生成表时hbm2ddl可以省略InterpreterRequest类的原因是什么?

1 个答案:

答案 0 :(得分:8)

您没有说明您正在使用哪个数据库,但问题可能是您的某个列名称或表名称是保留字。 from列绝对是所有数据库风格中的保留字。

修复:

您需要转义保留字的名称。这是mysql的解决方案,它使用后退标记将关键字组成文字):

 @Column(name = "`from`")
 private String from;

一个简单的测试,以确定某些东西是否为保留字是尝试手动创建表 - SQL解析器将快速告诉您问题所在。 检查其他列名称和表名称