在我的应用程序中,我有几个带有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 session 工厂配置:
<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类的原因可能是什么?
最佳答案
您没有说要使用哪个数据库,但是问题可能出在您的列名之一或表名是保留字。在所有数据库类型中,from
列绝对是保留字。
修理:
您需要转义保留字的名称。这是mysql的解决方案,它使用反斜线使关键字变成文字。
@Column(name = "`from`")
private String from;
找出是否是保留字的简单测试是尝试手动创建表-SQL解析器将快速告诉您问题出在哪里。
检查其他列名和表名