2013-02-10 109 views
2

我想通过使用Hibernate + JPA创建一个表。问题是,每当我运行我的代码时,它不会创建任何表。我已经创建了一个空的数据库。我使用Hibernate + JPA + HSQL + Maven。休眠与JPA不会创建表

这里是我的persistence.xml:

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> 

<!-- Provider of Persistence --> 
<provider>org.hibernate.ejb.HibernatePersistence</provider> 

<!-- Classes, in which JPA-Annotations are read --> 
<class>com.mysite.warehousebase.base.ProductData</class> 

<properties> 
    <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> 
    <property name="hibernate.connection.url" value="jdbc:hsqldb:Warehouse"/> 
    <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> 
    <property name="hibernate.connection.username" value="sa" /> 
    <property name="hibernate.connection.password" value="" /> 

    <property name="hibernate.show_sql" value="true"/> 
    <property name="hibernate.format_sql" value="true"/> 

    <property name="hibernate.hbm2ddl.auto" value="update"/> 
</properties> 
</persistence-unit> 
</persistence> 

这里是我的productData.java代码:

import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.Table; 
import org.hibernate.annotations.ForeignKey; 
import org.hibernate.validator.NotNull; 

@Entity 
@Table(name = "products") 
public class ProductData extends Entityclass { 

private static final long serialVersionUID = 1L; 

/* 
//Product Addition Time 
@NotNull 
@Column(name = "added", nullable = false) 
private Date productAdded;*/ 

//Product Name 
@NotNull 
@Column(name = "productname", nullable = false) 
private String productName; 

//Product Description 
@Column(name = "productdescription", nullable = true) 
private String productDescription; 

//Product Size 
@Column(name = "productsize", nullable = true) 
private String productSize; 

//Product Amount 
@NotNull 
@Column(name = "productamount", nullable = false) 
private int productAmount; 

//Product Left 
//Attribute to be calculated in own method 
@NotNull 
@Column(name = "productleft", nullable = false)  
private int productLeft; 

//Product buy price 
@NotNull 
@Column(name = "productprice", nullable = false)  
private double productPrice; 

//Total cost 
//Attribute to be calculated in own method 
@NotNull 
@Column(name = "totalproductprice", nullable = false) 
private double totalProductPrice; 

//Product sell price 
@NotNull 
@Column(name = "productsellprice", nullable = false) 
private double productSellPrice; 

//Product sale 
//Attribute to be calculated in own method 
@NotNull 
@Column(name = "totalsellprice", nullable = false)  
private double totalSellPrice; 

//Difference between cost and sale (total) 
//Attribute to be calculated in own method 
@NotNull 
@Column(name = "buyselldifference", nullable = false)  
private double buySellDifference; 

//Product status; ordered, at warehouse, reserved. 
//Attribute to be calculated in own method 
@NotNull 
@Column(name = "productstatus", nullable = false)  
private String productStatus; 

@Column(name = "reservedproducts", nullable = true) 
private int reservedProducts; 

/** 
* All Setters and Getters 
* 
*/ 

/** 
* @return the productName 
*/ 
public String getProductName() { 
    return productName; 
} 

/** 
* @param productName the productName to set 
*/ 
public void setProductName(String productName) { 
    this.productName = productName; 
} 

/** 
* @return the productDescription 
*/ 
public String getProductDescription() { 
    return productDescription; 
} 

/** 
* @param productDescription the productDescription to set 
*/ 
public void setProductDescription(String productDescription) { 
    this.productDescription = productDescription; 
} 

/** 
* @return the productSize 
*/ 
public String getProductSize() { 
    return productSize; 
} 

/** 
* @param productSize the productSize to set 
*/ 
public void setProductSize(String productSize) { 
    this.productSize = productSize; 
} 

/** 
* @return the productAmount 
*/ 
public int getProductAmount() { 
    return productAmount; 
} 

/** 
* @param productAmount the productAmount to set 
*/ 
public void setProductAmount(int productAmount) { 
    this.productAmount = productAmount; 
} 

/** 
* @return the productLeft 
*/ 
public int getProductLeft() { 
    return productLeft; 
} 

/** 
* @param productLeft the productLeft to set 
*/ 
public void setProductLeft(int productLeft) { 
    this.productLeft = productLeft; 
} 

/** 
* @return the productPrice 
*/ 
public double getProductPrice() { 
    return productPrice; 
} 

/** 
* @param productPrice the productPrice to set 
*/ 
public void setProductPrice(double productPrice) { 
    this.productPrice = productPrice; 
} 

/** 
* @return the totalProductPrice 
*/ 
public double getTotalProductPrice() { 
    return totalProductPrice; 
} 

/** 
* @param totalProductPrice the totalProductPrice to set 
*/ 
public void setTotalProductPrice(double totalProductPrice) { 
    this.totalProductPrice = totalProductPrice; 
} 

/** 
* @return the productSellPrice 
*/ 
public double getProductSellPrice() { 
    return productSellPrice; 
} 

/** 
* @param productSellPrice the productSellPrice to set 
*/ 
public void setProductSellPrice(double productSellPrice) { 
    this.productSellPrice = productSellPrice; 
} 

/** 
* @return the totalSellPrice 
*/ 
public double getTotalSellPrice() { 
    return totalSellPrice; 
} 

/** 
* @param totalSellPrice the totalSellPrice to set 
*/ 
public void setTotalSellPrice(double totalSellPrice) { 
    this.totalSellPrice = totalSellPrice; 
} 

/** 
* @return the buySellDifference 
*/ 
public double getBuySellDifference() { 
    return buySellDifference; 
} 

/** 
* @param buySellDifference the buySellDifference to set 
*/ 
public void setBuySellDifference(double buySellDifference) { 
    this.buySellDifference = buySellDifference; 
} 

/** 
* @return the productStatus 
*/ 
public String getProductStatus() { 
    return productStatus; 
} 

/** 
* @param productStatus the productStatus to set 
*/ 
public void setProductStatus(String productStatus) { 
    this.productStatus = productStatus; 
} 

    /** 
* @return the reservedProducts 
*/ 
public int getReservedProducts() { 
    return reservedProducts; 
} 

/** 
* @param reservedProducts the reservedProducts to set 
*/ 
public void setReservedProducts(int reservedProducts) { 
    this.reservedProducts = reservedProducts; 
} 

/** 
* @return the productSection 
*/ 
public ProductSection getProductSection() { 
    return productSection; 
} 

/** 
* @param productSection the productSection to set 
*/ 
public void setProductSection(ProductSection productSection) { 
    this.productSection = productSection; 
} 

public ProductData(){ 

} 

public ProductData(String productName){ 
    this.productName = productName; 
} 

} 

而且我main.java代码:

public static void main(String[] args) { 

    DataStructureTest testProduct = new DataStructureTest(); 
    testProduct.testProductData(); 
} 

有什么不对?为什么Hibernate不会创建一个需要的表?我曾尝试使用hibernate.hbm2ddl.auto,其值为“update”。它不会帮助。

回答

0

定义,我发现这个解决方案。对于

property name="hibernate.connection.url" value="jdbc:hsqldb:Warehouse"

某种原因,价值将无法正常工作。所以,而不是我所做的是我创建了一个数据库连接,它的值如下 jdbc:hsqldb:file:[folder name]

此文件夹名称是用户定义的。因此,该特定行的新代码将是:

property name="hibernate.connection.url" value="jdbc:hsqldb:file:[folder name]"

现在一切运行顺利并表创建和更新:)。

0

你可以尝试用

<property name="hibernate.hbm2ddl.auto" value="create"/> 

又“transaction-type="RESOURCE_LOCAL"在持久性XML

+0

是的,我已经尝试过你的建议,但它仍然无法正常工作。我有,但它仍然不起作用。 – 2013-02-21 12:01:11

+0

您可以尝试将jdbc url更改为'jdbc:hsqldb:Warehouse; create = true' – Sudhakar 2013-02-22 10:48:17