2

Hibernate won't create a specific table called "Product", even though it shows the create table command in the sql.

Category table gets created just fine...

These are classes ;

** BaseObject.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;
}

}

Category.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;
}


}

Product.java

@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;
}


}

Spring Conf

<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>

The product won't get created. The strange thing is, it puts the create table command in the sql...

    Hibernate: alter table Category drop foreign key FK_p6elut499cl32in8b8j8sy2n4
    Hibernate: alter table Product drop foreign key FK_b7afq93qsn7aoydaftixggf14
    Hibernate: drop table if exists Category
    Hibernate: drop table if exists Product
    Hibernate: create table Category (id bigint not null auto_increment, name varchar(255), parent_id bigint, primary key (id))
    Hibernate: create table Product (id bigint not null auto_increment, dataTableJson varchar(255), jargonCode varchar(255), order integer not null, category_id bigint, primary key (id))
    Hibernate: alter table Category add constraint FK_p6elut499cl32in8b8j8sy2n4 foreign key (parent_id) references Category (id)
    Hibernate: alter table Product add constraint FK_b7afq93qsn7aoydaftixggf14 foreign key (category_id) references Category (id)

I really don't understand why.. I use MySQL server. And I've tried setting hibernate.hbm2ddl.auto to both create and create-drop. But nothing. Can anyone help me ?

paroxit
  • 627
  • 2
  • 12
  • 30
  • this may be the issue due to use of incorrect dialect . Can you specify the dialect you are using for your MYSQL server. – Viraj Nalawade May 12 '15 at 13:42
  • org.hibernate.dialect.MySQLDialect – paroxit May 12 '15 at 13:48
  • That looks fine.. But still have a look at http://stackoverflow.com/questions/11291940/hibernate-does-not-create-tables-automatically if that helps also have a look at privileges of db user you are specifying in your config file. For me its not Hibernate issue there is something wrong related connection or related MYSQL.. – Viraj Nalawade May 12 '15 at 13:52
  • 1
    Thank you. But it was the Order column causing it.. – paroxit May 12 '15 at 14:09

1 Answers1

3

Try to change the 'order' column name, is a reserved word, formally the sql is correct by I had a similar issue calling a column 'user' instead of 'userId'

fantarama
  • 862
  • 6
  • 14