GORM组合 - 具有多对一关系的嵌入式域抛出org.hibernate.MappingException

Bec*_*ard 6 grails hibernate grails-orm

我试图在Grails 2.2.1中使用具有多对一关系的嵌入式域.这是我想要做的简化版本.

我正在映射到现有的db表:

create table incident (id bigint generated by default as identity, state_id bigint not null, primary key (id));
create table state (id bigint generated by default as identity, name varchar(255) not null, primary key (id));
alter table incident add constraint FK52F44D27499E79E foreign key (state_id) references state;
Run Code Online (Sandbox Code Playgroud)

映射到"事件"表的域:

class Incident {
    Vehicle vehicle
    static embedded = ['vehicle']
}

class Vehicle{
    State state
    static mapping = {
        state column: 'state_id'
    }   
}  
Run Code Online (Sandbox Code Playgroud)

映射到"状态"表的域:

class State {
    String name 
}
Run Code Online (Sandbox Code Playgroud)

当我尝试运行我的应用程序时,我收到以下错误:

消息:创建名为'transactionManagerPostProcessor'的bean时出错:bean的初始化失败; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'transactionManager'的bean时出错:在设置bean属性'sessionFactory'时无法解析对bean'sessionFactory'的引用; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'sessionFactory'的bean时出错:init方法的调用失败; 嵌套异常是org.hibernate.MappingException:无法确定:test.State的类型,在table:incident,对于列:[org.hibernate.mapping.Column(vehicle_state)]

是否可以在嵌入式域中建立多对一关联?

- 更新 -

我最终使用了一种解决方法来获得状态.

class Vehicle{

    static transients = [ "state" ]

    Long stateId

    static mapping = {
        stateId column: 'state_id'  
    }

    State getState(){
        State.get(this.stateId)
    }
}
Run Code Online (Sandbox Code Playgroud)

cod*_*ark 4

从 Grails 2.2.1 开始,嵌入式域类存在一些可用性问题,导致它们难以使用,尤其是在遗留数据库中。

  1. 关系不起作用
  2. 自定义列映射不起作用

您最好直接在所属类中映射列,然后创建一个辅助方法来处理嵌入的实体。

例如:

// grails-app/domain/yourpkg/Incident.groovy
class Incident {
    State state

    public Vehicle getVehicleData() {
        return new Vehicle(state: state)
    }

    public void updateWithVehicle(Vehicle vehicle) {
        state = vehicle.state
    }

    static mapping = {
        state column: 'state_id'
    }
}

// src/groovy/yourpkg/Vehicle.groovy
class Vehicle {
    State state
}
Run Code Online (Sandbox Code Playgroud)