In my application I have several entity POJOs annotated with JPA annotations. Also hbm2ddl is configured to generate tables for these entities. When application starts first time, all tables are generated successfully except one. Here's entity source code:
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开发者_如何学Go.event = event;
}
}
And here's hibernate session factory configuration:
<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>
What can be a reason for hbm2ddl to omit InterpreterRequest class when generating tables?
You didn't say which database you are using, but the problem is likely to be that one of your column names, or the table name, is a reserved word. The from
columns is definitely a reserved word in all database flavors.
To fix:
You need to escape the names that are reserved words. Here's the solution for mysql, which uses back ticks to make keywords into literals):
@Column(name = "`from`")
private String from;
A simple test to find out if something is a reserved word is to try to create the table manually - the SQL parser will quickly tell you where the problem is. Check other column names and the table name
精彩评论