gpt4 book ai didi

mysql - Hibernate 生成不正确的 MySQL 脚本

转载 作者:行者123 更新时间:2023-11-30 23:01:10 24 4
gpt4 key购买 nike

我有两个 Classes 将每个映射到 MySQL 数据库中的 Entity。每当我尝试映射到数据库时,我都会得到一个 MySQL Error

类(class)所有者:

@Entity
public class Owner {

@Id @GeneratedValue
private int idOwner;

public int getIdOwner() {
return idOwner;
}

public void setIdOwner(int idOwner) {
this.idOwner = idOwner;
}
}

带 FK 的类车:

@Entity
public class Car {

@Id @GeneratedValue
private int idCar;

@ManyToOne
@JoinColumn( name = "idOwner")
private Owner owner;

public int getIdCar() {
return idCar;
}

public void setIdCar(int idCar) {
this.idCar = idCar;
}

public Owner getOwner() {
return owner;
}

public void setOwner(Owner owner) {
this.owner = owner;
}
}

代码运行:

EntityManagerFactory f = Persistence.createEntityManagerFactory("glorious");
EntityManager em = f.createEntityManager();
EntityTransaction t = em.getTransaction();

t.begin();

Car c = new Car();
Owner o = new Owner();

c.setOwner(o);

em.persist(c);
em.persist(o);

t.commit();


f.close();
em.close();

错误:

GRAVE: Unsuccessful: alter table .Car add index FK107B43F620606 (idOwner), add constraint FK107B43F620606 foreign key (idOwner) references .Owner (idOwner)
27/05/2014 20:35:01 org.hibernate.tool.hbm2ddl.SchemaExport create
GRAVE: Can't create table 'glorious.#sql-2aa_1f8' (errno: 150)
  • MySQL 版本:5.5.34
  • 引擎:InnoDB
  • hibernate 方言:MySQL5InnoDBDialect

我把Hibernate生成的脚本直接在phpMyAdmin中测试了,没用。

SQL 脚本:

alter table .Car add index FK107B43F620606 (idOwner), add constraint FK107B43F620606 foreign key (idOwner) references .Owner (idOwner)

如果我通过将表名替换为 来修复脚本,例如Carglorious.Car 比它有效。有人有想法吗?

最佳答案

解决了。我的 persistence.xml 将属性 default_schema 设置为空。这就是为什么没有数据库名称出现在表名称之前的原因。

关于mysql - Hibernate 生成不正确的 MySQL 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23900721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com