0

Hibernate/jpa アノテーションを使用して、DDL スクリプトを自動作成/実行しています。

1 つのテーブル「UserAccount」とすべてのテーブルの外部キー「contraints」を除いて、すべてのテーブルが作成されています。

Apache Tomact のログを調べると、同様のエラーがいくつかあることに気付きました。

エラー出力:

SEVERE: 失敗: create table user_account (user_id bigint not null auto_increment, active bit, address_1 varchar(255), address_2 varchar(255), email varchar(255), first _name varchar(255), last_name varchar(255), phone_contact_1 varchar (255)、phone_contact_2 varchar(255)、user_type varchar(255)、Registeration_Code_fk bigint not null、主キー (user_id)) 2012 年 6 月 11 日 6:42:42 PM org.hibernate.tool.hbm2ddl.SchemaExport create

SEVERE: SQL 構文にエラーがあります。使用する正しい構文については、MySQL サーバーのバージョンに対応するマニュアルを確認してくださいPM org.hibernate.tool.hbm2ddl.SchemaExport 作成

SEVERE: 失敗: テーブル Indust_Of_Interest を変更し、インデックス FKBCDB75CAF90F35D9 (user_id) を追加し、制約 FKBCDB75CAF90F35D9 を追加します。外部キー (user_id) は、user_account (user_id) を参照します。

重大: テーブル 'yourmarketnet.#sql-584_86c' を作成できません (errno: 150)

など…</p>

1) 私の最初の質問は、休止状態に正しい SQL DDL 構文を生成させることです (正しい SQL 方言を与えられた休止状態は正しい SQL DLL を簡単に生成するはずなので、このエラーは奇妙です)?

2) 私の 2 番目の質問は、外部キー エラー (errno: 150) に関するものです。以前に DDL を手動で作成したときにこのエラー/バグを見たことがありますが、Hibernate 経由で修正するにはどうすればよいですか?

私の UserAccount クラス/エンティティ:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yourmarketnet.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 *
 * @author naim
 */
@Entity
@Table(name = "user_account")
public class UserAccount implements Serializable {

    @Autowired
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "user_account_seq")
    @SequenceGenerator(name = "user_account_seq", sequenceName = "user_account_seq")
    @Column(name = "user_id")
    private Long UserId = null;
    //
    @Autowired
    @Column(name = "user_type")
    private String UserType = null;
    //
    @Autowired
    @Column(name = "first _name")
    private String FirstName;
    //
    @Autowired
    @Column(name = "last_name")
    private String LastName;
    //
    @Autowired
    @Column(name = "email")
    private String Email;
    //
    @Autowired
    @Column(name = "phone_contact_1")
    private String PhoneContact1= null; 
    //
     @Autowired
    @Column(name = "phone_contact_2")
    private String PhoneContact2= null; 
    //
    @Autowired
    @Column(name = "address_1")
    private String Address_1 = null; 
    //
    @Autowired
    @Column(name = "address_2")
    private String Address_2 = null; 
    // 1 to many relation with industeries of interest 
    @Autowired
    @Column(name = "industeries_of_interest_set")
    @OneToMany (fetch = FetchType.LAZY, mappedBy="UserAccount" , cascade=CascadeType.ALL)
    private Set<IndusteriesOfInterest> IndusteriesOfInterestSet = new HashSet();
     // 1 to many relation with message posts per user account 
    @Autowired
    @Column(name = "message_posts_list")
    @OneToMany (fetch = FetchType.LAZY, mappedBy="UserAccount" , cascade=CascadeType.ALL)
    private List<MessagePost> MessagePostsList = new ArrayList<MessagePost>();
    //1 to many relation with inventory per user account 
    @Autowired
    @Column(name="inventory_list")
    @OneToMany (fetch = FetchType.LAZY, mappedBy="UserAccount" , cascade=CascadeType.ALL)
    private List<Inventory> InventoryList  = new ArrayList<Inventory>();
    //is the user account Active either due to user deactivation,admin deactivation, or nonpayment
    @Autowired
    @Column(name = "active")
    private boolean Active = false;
    //registerationCode relationship with UserRegisteration object 
    @Autowired
    @Qualifier("UserRegisteration")
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "Registeration_Code_fk", referencedColumnName="registeration_code", nullable = false)
    private UserRegisteration UserRegisteration;


    @Autowired(required = false)
    public UserAccount() {
    }

    @Autowired(required = true)
    public UserAccount(Long UserId, String FirstName, String LastName, String Email) {
        this.UserId = UserId;
        this.FirstName = FirstName;
        this.LastName = LastName;
        this.Email = Email;
    }


    private class Password {

        private UserAccount UserAccount = null;
        private String password = null;

        public Password(UserAccount UserAccount, String password) {
            this.UserAccount = UserAccount;
            this.password = password;
        }

        public UserAccount getUserAccount() {
            return UserAccount;
        }

        public String getPassword() {
            return password;
        }
    }

    public String getAddress_1() {
        return Address_1;
    }

    public void setAddress_1(String Address_1) {
        this.Address_1 = Address_1;
    }

    public String getAddress_2() {
        return Address_2;
    }

    public void setAddress_2(String Address_2) {
        this.Address_2 = Address_2;
    }

    public String getPhoneContact1() {
        return PhoneContact1;
    }

    public void setPhoneContact1(String PhoneContact1) {
        this.PhoneContact1 = PhoneContact1;
    }

    public String getPhoneContact2() {
        return PhoneContact2;
    }

    public void setPhoneContact2(String PhoneContact2) {
        this.PhoneContact2 = PhoneContact2;
    }

    public Set<IndusteriesOfInterest> getIndusteriesOfInterestSet() {
        return IndusteriesOfInterestSet;
    }

    public void setIndusteriesOfInterestSet(Set<IndusteriesOfInterest> IndusteriesOfInterestSet) {
        this.IndusteriesOfInterestSet = IndusteriesOfInterestSet;
    }

    public List<MessagePost> getMessagePostsList() {
        return MessagePostsList;
    }

    public void setMessagePostsList(List<MessagePost> MessagePostsList) {
        this.MessagePostsList = MessagePostsList;
    }

    public boolean isActive() {
        return Active;
    }

    public void setActive(boolean Active) {
        this.Active = Active;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String Email) {
        this.Email = Email;
    }

    public String getFirstName() {
        return FirstName;
    }

    public void setFirstName(String FirstName) {
        this.FirstName = FirstName;
    }

    public String getLastName() {
        return LastName;
    }

    public void setLastName(String LastName) {
        this.LastName = LastName;
    }

    public com.yourmarketnet.beans.UserRegisteration getUserRegisteration() {
        return UserRegisteration;
    }

    public void setUserRegisteration(com.yourmarketnet.beans.UserRegisteration UserRegisteration) {
        this.UserRegisteration = UserRegisteration;
    }

    public Long getUserId() {
        return UserId;
    }

    public void setUserId(Long UserId) {
        this.UserId = UserId;
    }

    public String getUserType() {
        return UserType;
    }

    public void setUserType(String UserType) {
        this.UserType = UserType;
    }

    public List<Inventory> getInventoryList() {
        return InventoryList;
    }

    public void setInventoryList(List<Inventory> InventoryList) {
        this.InventoryList = InventoryList;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final UserAccount other = (UserAccount) obj;
        if ((this.UserId == null) ? (other.UserId != null) : !this.UserId.equals(other.UserId)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 73 * hash + (this.UserId != null ? this.UserId.hashCode() : 0);
        return hash;
    }
}

hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/yourmarketnet</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">arya6678</property>
     <!--Enable this to see the SQL statements in the logs-->
     <property name="show_sql">true</property>    
      <!--This will drop our existing database and re-create a new one-->      
     <property name="hbm2ddl.auto">create</property> 
     <!--annotation beans/entity mappings-->
<mapping package="com.yourmarketnet.beans"/> 
<mapping class="com.yourmarketnet.beans.UserAccount"/>
<mapping class="com.yourmarketnet.beans.UserRegisteration"/>
<mapping class="com.yourmarketnet.beans.Product"/>
<mapping class="com.yourmarketnet.beans.Service"/>
<mapping class="com.yourmarketnet.beans.Inventory"/>
<mapping class="com.yourmarketnet.beans.IndusteriesOfInterest"/>
<mapping class="com.yourmarketnet.beans.MessagePost"/>
<mapping class="com.yourmarketnet.beans.Offering"/>
<mapping class="com.yourmarketnet.beans.OfferingImage"/>
  </session-factory>
</hibernate-configuration>
4

1 に答える 1

0

列に誤ったスペースがありますfirst_name。このuser_accountエラーのため、テーブルの作成に失敗します。その後、参照されるテーブルが存在しないため、参照する外部キーuser_accountを作成できません。

 @Autowired
 @Column(name = "first _name")
 // ------------------^^^^
 // Remove the space!
 private String FirstName;

_nameエラー メッセージは の直前に発生した を指しているlast_nameので、そこから問題を探し始めます。MySQL は通常、構文エラーの正確な文字位置について非常に説明的です。

SQL 構文にエラーがあります。'_name varchar(255), l の近くで使用する正しい構文については、MySQL サーバーのバージョンに対応するマニュアルを確認してください。

于 2012-06-12T02:33:52.017 に答える