0

我正在使用 hibernate/jpa 注释来自动创建/执行 DDL 脚本。

我的所有表都在创建,除了一个表“UserAccount”和所有表上的外键“约束”。

在检查 Apache Tomact 日志时,我注意到几个错误类似的错误:

错误输出:

严重:不成功:创建表 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 不为空,主键 (user_id)) 2012 年 6 月 11 日下午 6:42:42 org.hibernate.tool.hbm2ddl.SchemaExport 创建

严重:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在 2012 年 6 月 11 日 6:42:42 的第 1 行 '_name varchar(255), last_name varchar(255), phone_contact_1 varchar(255), phone_' 附近使用正确的语法PM org.hibernate.tool.hbm2ddl.SchemaExport 创建

严重:不成功:更改表 Indust_Of_Interest 添加索引 FKBCDB75CAF90F35D9 (user_id),添加约束 FKBCDB75CAF90F35D9 外键 (user_id) 引用 user_account (user_id) 2012 年 6 月 11 日下午 6:42:42 org.hibernate.tool.hbm2ddl.SchemaExport create

严重:无法创建表'yourmarketnet.#sql-584_86c'(错误号:150)

等等……</p>

1)我的第一个问题是让hibernate生成正确的SQL DDL语法(这个错误很奇怪,因为Hibernate给出正确的SQL方言应该很容易生成正确的SQL DLL)?

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

休眠.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 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在 '_name varchar(255), l 附近使用的正确语法

于 2012-06-12T02:33:52.017 回答