2012-06-12 61 views
0

我使用hibernate/jpa註釋來自動創建/執行DDL腳本。休眠SQL DDL腳本錯誤150,無法使外鍵約束

我所有的表格都在創建,除了所有表格上的一個表「UserAccount」和外鍵「contraints」。

當檢查了Apache Tomact登錄我發現了幾個錯誤類似的錯誤:

錯誤輸出:

重度:不成功:創建表USER_ACCOUNT(user_ID的BIGINT NOT NULL AUTO_INCREMENT,有效位,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,primary key(user_id)) 2012年6月11日下午6:42:42 org.hibernate.tool.hbm2ddl.SchemaExport創建

嚴重:你的SQL語法有錯誤;請檢查與您的MySQL服務器版本相對應的手冊,以便在第1行'_name varchar(255),last_name varchar(255),phone_contact_1 varchar(255),phone_'附近使用正確的語法。 Jun 11,2012 6:42: 42 PM org.hibernate.tool.hbm2ddl.SchemaExport創建

重度:不成功:ALTER TABLE Indust_Of_Interest添加索引FKBCDB75CAF90F35D9(USER_ID),加約束FKBCDB75CAF90F35D9外鍵(USER_ID)引用USER_ACCOUNT(USER_ID) 2012年6月11日6: 42:42 PM org.hibernate.tool.hbm2ddl.SchemaExport創建

重度:無法創建表 'yourmarketnet#SQL-584_86c'(錯誤:150)

等等......

1)我的第一個問題是讓hibernate生成正確的SQL DDL語法(這個錯誤很奇怪,因爲Hibernate給出正確的SQL方言應該很容易生成正確的SQL DLL)?我的第二個問題是關於外鍵錯誤(errno:150),我在手工創建DDL之前看到過這個錯誤/錯誤,但是我怎麼能通過Hibernate修復它?

我UserAcccount類/實體:

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

回答

0

您的first_name列一個錯誤的空間。由於此錯誤,無法創建user_account表。之後,由於引用的表不存在,因此無法創建引用user_account的外鍵。

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

錯誤消息指出發生之前last_name_name,所以這是開始尋找問題的地方。 MySQL通常對描述語法錯誤的確切字符位置很有描述:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '_name varchar(255), l