即使它在sql中显示,Hibernate也不会创建表

时间:2015-05-12 13:39:26

标签: java mysql spring hibernate

Hibernate不会创建一个名为“Product”的特定表,即使它在sql中显示了create table命令。

分类表创建得很好......

这些是类;

** BaseObject.java **

    $userIds = array(1,3);
    $c = Conversation::whereHas('users', function($q) use ($userIds)
         {
             $q->whereIn('user_id', $userIds);
         })
        ->get();

Category.java

@MappedSuperclass
public class BaseObject implements Serializable 
{
public static final long serialVersionUID = 1L;

@Id
@GeneratedValue
private Long id;


public Long getId() {
    return id;
}

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

}

Product.java

@Entity
public class Category extends BaseObject {

private String name;

@OneToMany(fetch=FetchType.LAZY,mappedBy="category",orphanRemoval=true)
private List<Product> products;

@OneToOne(optional=true)
private Category parent;



public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}


public List<Product> getProducts() {
    return products;
}

public void setProducts(List<Product> products) {
    this.products = products;
}

public Category getParent() {
    return parent;
}

public void setParent(Category parent) {
    this.parent = parent;
}


}

Spring Conf

@Entity
public class Product extends BaseObject{


@ManyToOne
private Category category;

private String jargonCode;

private int order;


private String dataTableJson;


public Category getCategory() {
    return category;
}

public void setCategory(Category category) {
    this.category = category;
}



public String getDataTableJson() {
    return dataTableJson;
}

public void setDataTableJson(String dataTableJson) {
    this.dataTableJson = dataTableJson;
}

public String getJargonCode() {
    return jargonCode;
}

public void setJargonCode(String jargonCode) {
    this.jargonCode = jargonCode;
}

public int getOrder() {
    return order;
}

public void setOrder(int order) {
    this.order = order;
}


}

产品无法创建。奇怪的是,它将create table命令放在sql ...

<jee:jndi-lookup id="dataSource" jndi-name="${jdni.dataSource}"
    expected-type="javax.sql.DataSource" />


<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.dataCollector.pojo.Category</value>
            <value>com.dataCollector.pojo.Product</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.showSql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.action}</prop>
        </props>
    </property>
</bean>

我真的不明白为什么..我使用MySQL服务器。 我已经尝试将hibernate.hbm2ddl.auto设置为create和create-drop。但没什么。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:3)

尝试更改&#39;命令&#39;列名,是一个保留字,正式的sql是正确的我有一个类似的问题,调用列&#39;用户&#39;而不是&#39; userId&#39;