2016-05-12 71 views
1

我一直在嘗試使用MYSQL添加約束UNSIGNED到列。下面是我的桌子。在休眠INT(11)UNSIGNED約束

public class UserActivity implements BaseEntity{ 

@Id 
@GeneratedValue(strategy = GenerationType.AUTO) 
Long id; 

Long entityId; 

@Column(columnDefinition = "enum('QUESTION','ANSWER', 'COMMENT', 'TAGS')") 
@Enumerated(EnumType.STRING) 
Type type; 

@ManyToOne 
@JoinColumn(nullable = false, columnDefinition="INT(11) UNSIGNED") 
CustomerEntity user; 

但它不創造因的外鍵和索引沒有建立數據庫約束用戶列。 Thisthis忍不住。請幫助我。感謝

原木:

Hibernate: 
alter table UserActivity 
    add constraint FK_jfw954ii6gw7mbqdth6y3n0rs 
    foreign key (user_Id) 
    references customerDb.customer (Id) 
12:31:30.350 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table UserActivity add constraint FK_jfw954ii6gw7mbqdth6y3n0rs foreign key (user_Id) references customerDb.customer (Id) 
12:31:30.350 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaExport - Can't create table 'community_db.#sql-43f0_5a07' (errno: 150) 
Hibernate: 

MySQL的查詢:

ALTER TABLE `community_db`.`Question` 
ADD INDEX `fk_Question_Customer_idx` (`author_Id` ASC); 
ALTER TABLE `community_db`.`Question` 
ADD CONSTRAINT `fk_Answer_Customer` 
FOREIGN KEY (`author_Id`) 
REFERENCES `customerDb`.`customer` (`Id`) 
ON DELETE NO ACTION 
ON UPDATE NO ACTION; 

和下面是上述MySQL的查詢日誌:

Error Code: 1005. Can't create table 'community_db.#sql-43f0_5d19' (errno: 150) 0.048 sec 
+1

你能發佈你得到的實際錯誤嗎? –

+0

在我搜索'UNSIGNED'的日誌中找不到。沒有查詢UNSIGNED和show_sql和format_sql爲真 –

+0

@TimBiegeleisen我已經添加了來自日誌的查詢 –

回答

3

@JoinColumncolumnDefinition屬性值或外鍵必須等於存在於實體Id中的@Column註釋的3210屬性值,即它們必須具有相同的屬性。

下面是CustomerEntity

@PersistenceUnit(unitName="persistenceUnit") 
@Entity 
@Table(name = "customer", schema = "customerDb") 
public class CustomerEntity { 
    @Id 
    @Column(name = "Id", nullable = false, columnDefinition = "INT(11) UNSIGNED") 
    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 
.................. 
} 

並在其中將包含客戶ID爲外國 鍵相同的同一columndDefinition必須存在於UserActivity。

public class UserActivity implements BaseEntity { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    Long id; 

    Long entityId; 

    @ManyToOne 
    @JoinColumn(nullable = false, columnDefinition = "INT(11) UNSIGNED") 
    CustomerEntity user; 
.................. 
} 

我用IntelliJIdea創建了那個表,但沒有添加這些屬性,因爲我得到了錯誤。