Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JPA/Hibernate——实体名称似乎很重要。如果我改名为";“鲍勃”;很好_Hibernate_Jpa - Fatal编程技术网

JPA/Hibernate——实体名称似乎很重要。如果我改名为";“鲍勃”;很好

JPA/Hibernate——实体名称似乎很重要。如果我改名为";“鲍勃”;很好,hibernate,jpa,Hibernate,Jpa,这个问题已经困扰了我好几个小时了。这与我的另一个问题有关: 我去掉了几乎所有的代码,缩小了问题的范围。我有三个非常简单的实体: @Entity public class Building { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; public Building() { } public Long getId() { return

这个问题已经困扰了我好几个小时了。这与我的另一个问题有关:

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

@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;
    }
}
这将因外键约束冲突而失败:

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)
现在这里是真正奇怪的部分!!!!如果我进行eclipse重构并将“其他”实体重命名为“Bob”,那么它工作得非常好。正如我们在同一页上一样,事件实体现在将如下所示:

@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;
    }
}
现在它工作得非常好。有人能给我这样的冬眠新手解释一下吗?这真让我难堪。将新事件持久化到我的服务层中的数据库时失败:

@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);
}
还有一点意见。除了将Other重命名为Bob,我还可以将表名设置为小写:

@Entity
@Table(name = "other")
public class Other {
这也解决了这个问题。但我不能接受这些解决方案。我想知道为什么会失败。我做了一个技术决定,不使用Grails来避免这种“魔法”

以下是SQL hibernate日志:

// 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]

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


似乎mysql、hibernate和MacOSX的结合导致了这个问题。作为解决方案,使用小写命名策略。

哪个实体映射到表blah?我想知道hibernate是否正在生成一个以mysql关键字作为其中一列名称的查询。可以添加hibernate生成的查询吗?blah是数据库名称。我将查询添加到最初的帖子中。谢谢你的帮助!我在Mac OS席上运行MySQL 5.1找到了解决方案。似乎是MacOSX、hibernate和mysql的组合导致了这个问题。见: