I’m using hibernate/jpa annotations to auto create/execute DDL scripts.
All my tables are being created except one table “UserAccount” and the foreign key “contraints” on all tables.
When inspecting the Apache Tomact log I noticed several error similar errors:
Error Output:
SEVERE: Unsuccessful: 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, primary key (user_id)) Jun 11, 2012 6:42:42 PM org.hibernate.tool.hbm2ddl.SchemaExport create
SEVERE: 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), last_name varchar(255), phone_contact_1 varchar(255), phone_' at line 1 Jun 11, 2012 6:42:42 PM org.hibernate.tool.hbm2ddl.SchemaExport create
SEVERE: Unsuccessful: alter table Indust_Of_Interest add index FKBCDB75CAF90F35D9 (user_id), add constraint FKBCDB75CAF90F35D9 foreign key (user_id) references user_account (user_id) Jun 11, 2012 6:42:42 PM org.hibernate.tool.hbm2ddl.SchemaExport create
SEVERE: Can't create table 'yourmarketnet.#sql-584_86c' (errno: 150)
Etc…
1) My 1st question is to have hibernate generate correct SQL DDL syntax (This error is strange since the Hibernate given correct SQL dialect should easily generate correct SQL DLL)?
2) My 2nd question is regarding foreign key error (errno: 150), I have seen this error/bug before when create DDL manually, but how can I fix it via Hibernate?
My UserAcccount Class/Entity:
/*
* 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>