GORM Composition - An embedded domain with many-to-one relationships throws org.hibernate.MappingException

I am trying to use an inline domain with many relationships in Grails 2.2.1. Here is a simplified version of what I'm trying to do.

I map existing db tables:

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; 

Domains that map to the incident table:

 class Incident { Vehicle vehicle static embedded = ['vehicle'] } class Vehicle{ State state static mapping = { state column: 'state_id' } } 

The domain that appears in the status table:

 class State { String name } 

When I try to run my application, I get the following error:

Message: Error creating bean named "transactionManagerPostProcessor": Initialization of bean failed; The nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean named "transactionManager": unable to resolve bean reference to "sessionFactory" when setting the bean property "sessionFactory"; The nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean named "sessionFactory": init method call failed; nested exception org.hibernate.MappingException: could not determine the type for: test.State, at table: incident, for columns: [org.hibernate.mapping.Column (vehicle_state)]

Is it possible to have a multi-valued connection in the embedded domain?

- Update -

I ended up using a workaround to get the state.

 class Vehicle{ static transients = [ "state" ] Long stateId static mapping = { stateId column: 'state_id' } State getState(){ State.get(this.stateId) } } 
+6
source share
2 answers

As in Grails 2.2.1, the built-in domain classes have several usability problems that make them difficult to work with, especially in older databases.

  • Relationships do not work.
  • Custom column mappings do not work.

You better match the column directly in your own class, and then create a helper method to handle inline objects.

eg:.

 // 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 } 
+4
source

In this case, the domains must be serialized (including the built-in Vehicle , which will not be part of the save if ported to src/groovy ) and will generate equals() hashCode() for the classes. Old school, but it works well, the application works properly.

Incedent.groovy

 import groovy.transform.EqualsAndHashCode @EqualsAndHashCode class Incedent implements Serializable{ Vehicle vehicle static embedded = ['vehicle'] class Vehicle{ State state } } 

State.groovy

 import groovy.transform.EqualsAndHashCode @EqualsAndHashCode class State implements Serializable { String name } 

Following the above, you will get a column in Incedent for embedding with the VARBINARY data type (when testing in memory H2 db). I would rather have stateName as a string in Vehicle

enter image description here

0
source

All Articles