Hibernate生成错误的MySQL脚本

时间:2014-05-27 23:46:35

标签: mysql hibernate

我有两个Classes每个Entity映射到MySQL数据库中的MySQL。每当我尝试映射到数据库时,我都会得到Error @Entity public class Owner { @Id @GeneratedValue private int idOwner; public int getIdOwner() { return idOwner; } public void setIdOwner(int idOwner) { this.idOwner = idOwner; } }

班主:

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

使用FK的Class Car:

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)

错误:

Hibernate
  • MySQL版本:5.5.34
  • 引擎:InnoDB
  • Hibernate方言:MySQL5InnoDBDialect

我使用alter table .Car add index FK107B43F620606 (idOwner), add constraint FK107B43F620606 foreign key (idOwner) references .Owner (idOwner) 生成的脚本并直接在phpMyAdmin中测试它,但它不起作用。

SQL脚本:

Car

如果我通过用例如替换表名来修复脚本,例如glorious.Car或{{1}}比它有效。有人有想法吗?

1 个答案:

答案 0 :(得分:0)

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