JPA / Hibernate - 实体名称似乎很重要。如果我重命名为“Bob”工作正常 [英] JPA/Hibernate - Entity name seems to be important. If I rename to "Bob" works fine

查看:147
本文介绍了JPA / Hibernate - 实体名称似乎很重要。如果我重命名为“Bob”工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一次让我陷入了几个小时。这与我在这里的其他问题有关:

$ b更新子行
$ b

我摆脱了几乎所有的代码并缩小了问题范围。我有三个非常简单的实体:

  @Entity 
public class Building {

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

public Building(){
}

public Long getId(){
return id;
}

public void setId(Long id){
this.id = id;


$ / code>

Other.java

  @Entity 
public class其他{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
私人长ID;

@ManyToOne
@JoinColumn(name =building_id)
私人建筑物;
$ b $ public其他(){
}

public Long getId(){
return id;
}

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

public Building getBuilding(){
return building;
}

public void setBuilding(建筑学校){
this.building = school;


$ / code $ / pre
$ b $ Event.java

  @Entity 
public class Event {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
私人长ID;

@ManyToOne
@JoinColumn(name =other_id)
private其他;


public long getId(){
return id;
}

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

public其他getOther(){
return other;
}

public void setOther(other other){
this.other = other;




$ b

这会失败并且违反外键约束:

$ b $> / p>

 原因:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败(`blah`.`event`,CONSTRAINT`FK403827A76D0546B` FOREIGN KEY(`other_id`)REFERENCES`Other`(`id`))
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)

现在这是真正的奇怪的部分!如果我做了一个eclipse重构并重命名其他实体鲍勃,它的工作原理非常好。如果我们在同一页面上,Event实体现在看起来像这样:

  @Entity 
public class Event {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
私人长ID;

@ManyToOne
@JoinColumn(name =bob_id)
私人Bob bob;


public long getId(){
return id;
}

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

public Bob getBob(){
return bob;
}

public void setBob(Bob bob){
this.bob = bob;
}
}

现在它工作得很好。有人可以解释一下像我这样的Hibernate新手吗?这真的让我难住。当我在服务层中将新事件持久化到数据库时失败:

  @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public void addEvent(Event event)throws ErrorCodeException,Exception
{
//不知道如果我需要这样做,仍然失败,如果我删除它
event.setOther(otherDAO.getReferenceById(event.getOther()的getId())。);
eventDAO.persist(event);
}

还有一点评论。而不是将其他人重命名为Bob,我也可以将表名设置为小写:

  @Entity 
@Table )
public class其他{

这也同样解决了这个问题。但我不能接受这些解决方案。我想知道它为什么失败。我做了一个技术决定,不使用Grails来避免这种魔力。



这里是SQL hibernate日志:


$ b $ ()
Hibernate:插入到Building值()
Hibernate:插入到其他(building_id)值(?)

//插入抛出异常
Hibernate:插入Event(other_id)值(?)
2011-06-27 11:31:51 JDBCExceptionReporter [WARN] SQL错误:1452,SQLState :23000
2011-06-27 11:31:51 JDBCExceptionReporter [错误]无法添加或更新子行:外键约束失败(`blah`.`event`,CONSTRAINT`FK403827A76D0546B` FOREIGN KEY(` other_id`)参考`其他`(`id`))
org.springframework.orm.jpa.JpaSystemException:org.hibernate.exception.ConstraintViolationException:无法插入:[com.blah.server.domain.Event] ;嵌套异常是javax.persistence.PersistenceException:org.hibernate.exception.ConstraintViolationException:无法插入:[com.blah.server.domain.Event]


解决方案

更新:我在这里找到了解决方案:

http: //developmentality.wordpress.com/2011/05/23/hibernate-mysql-mac-foreign-key-nightmares-a-painless-solution-to-a-painful-problem/



似乎mysql,hibernate和mac os x的组合导致了这个问题。作为解决方案,请使用小写字母命名策略。


This one has had me stumped for several hours. It's related to my other question here:

JPA/hibernate - Cannot add or update a child row: a foreign key constraint fails - BUT record exists

I got rid of nearly all the code and narrowed down the problem. I have three very simple entities:

@Entity
public class Building {

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

    public Building() {
    }

    public Long getId() {
        return id;
    }

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

Other.java

@Entity
public class Other {

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

    @ManyToOne
    @JoinColumn(name = "building_id")
    private Building building;

    public Other() {
    }

    public Long getId() {
        return id;
    }

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

    public Building getBuilding() {
        return building;
    }

    public void setBuilding(Building school) {
        this.building = school;
    }
}

Event.java

@Entity
public class Event {

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

    @ManyToOne
    @JoinColumn(name = "other_id")
    private Other other;


    public long getId() {
        return id;
    }

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

    public Other getOther() {
        return other;
    }

    public void setOther(Other other) {
        this.other = other;
    }
}

This will fail with a foreign key constraint violation:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`blah`.`event`, CONSTRAINT `FK403827A76D0546B` FOREIGN KEY (`other_id`) REFERENCES `Other` (`id`))
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)

NOW HERE'S THE REALLY BIZARRE PART!!!! If I do an eclipse refactor and rename the "Other" entity "Bob" it works perfectly fine. Just so we're on the same page, the Event entity will now look like this:

@Entity
public class Event {

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

    @ManyToOne
    @JoinColumn(name = "bob_id")
    private Bob bob;


    public long getId() {
        return id;
    }

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

    public Bob getBob() {
        return bob;
    }

    public void setBob(Bob bob) {
        this.bob = bob;
    }
}

And now it works perfectly fine. Can someone please explain this to a Hibernate newbie like myself? This really has me stumped. It fails when I persist a new Event to the database in my service layer:

@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
public void addEvent(Event event) throws ErrorCodeException, Exception
{
    // not sure if I need to do this, still fails if I remove it
    event.setOther(otherDAO.getReferenceById(event.getOther().getId()));
    eventDAO.persist(event);
}

One further comment. Instead of renaming Other to Bob, I can also set the table name to lowercase:

@Entity
@Table(name = "other")
public class Other {

This ALSO fixes it. But I cannot accept these solutions. I want to know why it's failing. I made a technology decision not to use Grails to avoid this type of "magic".

Here's the SQL hibernate logs:

// Initial database inserts
Hibernate: insert into Building values ( )
Hibernate: insert into Other (building_id) values (?)

// Insert which throws exception
Hibernate: insert into Event (other_id) values (?)
2011-06-27 11:31:51 JDBCExceptionReporter [WARN] SQL Error: 1452, SQLState: 23000
2011-06-27 11:31:51 JDBCExceptionReporter [ERROR] Cannot add or update a child row: a foreign key constraint fails (`blah`.`event`, CONSTRAINT `FK403827A76D0546B` FOREIGN KEY (`other_id`) REFERENCES `Other` (`id`))
org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.ConstraintViolationException: could not insert: [com.blah.server.domain.Event]; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not insert: [com.blah.server.domain.Event]

解决方案

UPDATE: I found the solution here:

http://developmentality.wordpress.com/2011/05/23/hibernate-mysql-mac-foreign-key-nightmares-a-painless-solution-to-a-painful-problem/

It seems the combination of mysql, hibernate, and mac os x cause this problem. As a solution, use a lowercase naming strategy.

这篇关于JPA / Hibernate - 实体名称似乎很重要。如果我重命名为“Bob”工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆