当前位置: 首页 > 知识库问答 >
问题:

Hibernate SQL DDL脚本错误150,无法设置外键约束

轩辕弘雅
2023-03-14

严重:不成功:创建表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,primary key(user_id)),2012年

严重:您的SQL语法出现错误;请查看与您的MySQL服务器版本相对应的手册,了解在“_name varchar(255),last_name varchar(255),phone_contact_1 varchar(255),phone_'”处使用的正确语法,该语法位于2012年6月11日6:42:42 PM org.hibernate.tool.hbm2ddl.schemaexport create

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

严重:无法创建表'YourMarketNet.#SQL-584_86C'(错误号:150)

等…

我的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>

共有1个答案

温成济
2023-03-14

first_name列中有一个错误的空格。由于此错误,user_account表无法创建。稍后,由于所引用的表不存在,无法创建引用user_account的外键。

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

错误消息指向在last_name之前出现的_name,因此可以从这里开始查找问题。MySQL通常对语法错误的确切字符位置有很好的描述性:

您的SQL语法中有一个错误;请查看与您的MySQL服务器版本相对应的手册,查找在'_name varchar(255),l附近使用的正确语法

 类似资料:
  • 问题内容: 当我执行以下两个查询时(我将它们简化为绝对必要): 我收到以下错误:错误1005(HY000):无法创建表’./test/bar.frm’(errno:150) 我的错误在哪里?半个小时盯着我看,我还没找到他。 问题答案: 来自约束 如果重新创建已删除的表,则该表必须具有符合引用该表的外键约束的定义。如前所述,它必须具有正确的列名和类型,并且必须在引用的键上具有索引。如果不满足这些条件

  • 我正在尝试迁移Rails MySQL数据库,出现以下错误: ActiveRecord::声明无效:MySQL2::Error:不能创建表.(errno: 150"外键约束格式不正确"): CREATE TABLE(intAUTO_INCREMENTPRIMary关键,varchar(255),int,int,datetime非空,datetime非空,INDEX(),INDEX(),CONSTRA

  • 问题内容: 我正在用MySQL Workbench做一个小型数据库。我有一个名为“ Immobili”的主表,它具有由四列组成的主键:(Comune,Via,Civico,Immobile)。 现在,我还有其他三个表,它们具有相同的主键(Comune,Via,Civico,Immobile),但是这些字段也引用了表Immobili。 第一个问题:我可以同时创建主键还是外键吗? 第二个问题:当我尝试

  • 我有两张桌子。第一个是人口;第二个是空的。 我希望第二个有一个外键,它引用第一个中的一列。 null 架构: 填充。为空。 不能成为的外键: MySQL没有提供错误的原因;不返回: 我拥有所需的数据库权限。 我已经仔细检查了列(甚至表)是否具有相同的排序规则(字符集不适用于INT列): MySQL无法添加外键约束 MySQL:错误1215(HY000):无法添加外键约束 无法添加外键约束-MySQ

  • 表a已成功创建... 错误1215(HY000):无法添加外键约束 无法创建表b

  • 问题内容: 我正在尝试通过使用外键来确保表之间的数据一致性,以便DBMS可以检查错误。但是,由于某些原因,我们似乎无法做到这一点。有什么错误,还有替代方法吗?另外,当我填写具有外键的表时,无法填写为外键保留的字段,对吗?另外,外键是否完全视为键? 问题答案: 最可能的问题是此行: classLeader的数据类型为VARCHAR(255)。这有 匹配数据类型 的引用的列… 。当然,表必须存在,列也