如果它不存在则创建表:JPA 2

时间:2016-05-23 12:01:31

标签: hibernate tomcat jpa

interval type主题(特别是在评论中),我知道使用

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

而不是<dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>3.6.10.Final</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.2.0.Final</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> </dependencies>

实际上是根据我项目中的@Entity类创建表。我正在遵循这一点,但我仍然收到以下错误。

  

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:Table   'testdb.CUST_TABLE'不存在

以下是我的pom.xml,用于查看我正在使用的不同依赖项的版本。

@Entity
@Table(name="CUST_TABLE")
public class Customer implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="CUST_ID", nullable=false)
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long custId;

    @Column(name="FIRST_NAME", nullable=false, length=50)
    private String firstName;

    @Column(name="LAST_NAME", length=50)
    private String lastName;

    //getters and setters
}

有人能指出我遇到的问题或误解吗?

修改

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
    <persistence-unit name="testjpa">
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost/testdb" />
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.username" value="nabin" />
            <property name="hibernate.connection.password" value="nabin" />
            <property name="hibernate.Log" value="SQL=TRACE" />
            <property name="hibernate.hbm2ddl.auto" value="update" /> 
            <property name="javax.persistence.schema-generation.database.action" value="create"/>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />

        </properties>
    </persistence-unit>
</persistence>

我的persistence.xml文件是:

    private static String sendPost(String url, Map<String, JSONObject> params) throws Exception {
    String responseString;

    StringBuilder urlParameters = new StringBuilder(400);
    if (params != null) {
        for (Entry<String, JSONObject> entry : params.entrySet()) {
            urlParameters.append(entry.getKey()).append("=").append(entry.getValue().toString()).append("&");
        }

    }
    url += urlParameters.toString();
    url = url.replace(" ", "%20");
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("POST");
    con.setRequestProperty("charset", "utf-8");
    con.setDoOutput(true);
    int responseCode = con.getResponseCode();
    if (responseCode == HttpStatus.SC_OK) {
        BufferedReader in = null;
        StringBuffer response = null;
        try{
            //when i check 'con' all seems to be fine
            in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
            String inputLine;
            response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
        }finally{
            in.close();
        }
        responseString = response.toString();
    } else {
        responseString = new StringBuilder(25).append(responseCode).toString();
    }
    return responseString;
}

0 个答案:

没有答案