gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:24:05 25 4
gpt4 key购买 nike

我正在尝试在 Grails 2.2.1 中使用具有多对一关系的嵌入式域。这是我正在尝试做的事情的简化版本。

我正在映射到现有的数据库表:

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;

映射到“事件”表的域:
class Incident {
Vehicle vehicle
static embedded = ['vehicle']
}

class Vehicle{
State state
static mapping = {
state column: 'state_id'
}
}

映射到“状态”表的域:
class State {
String name
}

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

Message: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not determine type for: test.State, at table: incident, for columns: [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)
}
}

最佳答案

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

  • 关系不起作用
  • 自定义列映射不起作用

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

    例如。:
    // 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
    }

    关于hibernate - GORM 组合 - 具有多对一关系的嵌入式域引发 org.hibernate.MappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16047527/

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