【问题标题】:MappingException trying to use POJO for domain object in GrailsMappingException 尝试将 POJO 用于 Grails 中的域对象
【发布时间】:2012-05-05 00:15:00
【问题描述】:

我有几个 POJO:

public class FlightBase {

    public AirportBase getDeparture() {
        return departure;
    }

    public void setDeparture(AirportBase departure) {
        this.departure = departure;
    }

    public AirportBase getDestination() {
        return destination;
    }

    public void setDestination(AirportBase destination) {
        this.destination = destination;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }
    String details = "";
    AirportBase departure;
    AirportBase destination;
}

public class AirportBase {
    String icaoIdentifier = "KSPG";

    public String getIcaoIdentifier() {
        return icaoIdentifier;
    }

    public void setIcaoIdentifier(String icaoIdentifier) {
        this.icaoIdentifier = icaoIdentifier;
    }

}

...我正在尝试通过扩展它们将它们用作 grails 应用程序中的域类:

package flightloggrails

    import com.flightloglib.domain.AirportBase
    import com.flightloglib.domain.FlightBase

    class Flight extends FlightBase  {

        static hasOne = [departure:AirportBase, destination:AirportBase ]

        static mapping = {

            departure type: Airport
            destination type: Airport
        }


        static constraints = {
            details( blank:false,null:false, widget:'textarea')
        }
    }

还有这个域类:

package flightloggrails

import com.flightloglib.domain.AirportBase

class Airport extends AirportBase {

    static constraints = {
    }
}

...继续尝试执行“run-app”(前面是“clean”):

 Error 2012-04-24 06:50:18,892 [pool-5-thread-1] ERROR context.GrailsContextLoader  - Error executing bootstraps: 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: flightloggrails.Airport, at table: flight, for columns: [org.hibernate.mapping.Column(departure)]
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: flightloggrails.Airport, at table: flight, for columns: [org.hibernate.mapping.Column(departure)]
   Line | Method
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask
|   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run      in     ''
^   662 | run . .  in java.lang.Thread

Caused by 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: flightloggrails.Airport, at table: flight, for columns: [org.hibernate.mapping.Column(departure)]
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask
|   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run      in     ''
^   662 | run . .  in java.lang.Thread

Caused by BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not determine type for: flightloggrails.Airport, at table: flight, for columns: [org.hibernate.mapping.Column(departure)]
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask
|   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run      in     ''
^   662 | run . .  in java.lang.Thread

Caused by MappingException: Could not determine type for: flightloggrails.Airport, at table: flight, for columns: [org.hibernate.mapping.Column(departure)]
->> 303 | innerRun in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   138 | run      in java.util.concurrent.FutureTask
|   886 | runTask  in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run      in     ''
^   662 | run . .  in java.lang.Thread

【问题讨论】:

  • 我不是 100% 确定,但我相信域类必须扩展另一个域类,而不是 POJO/POGO。不过我可能是错的。
  • 在我尝试添加映射之前一切似乎都很好。否则,Flight 类的“全部生成”可以很好地创建所有 CRUD 工件。
  • 这个问题你解决了吗?
  • 是的,终于想通了,有一段时间了,如果你需要我可以挖掘它

标签: hibernate grails mapping pojo


【解决方案1】:

似乎有更好的方法来做到这一点:

A good question & answer on the stackoverflow

The Grails documentation

【讨论】:

    【解决方案2】:

    您正在将多对一关系映射到非域类。

    class Flight extends FlightBase  {
        static hasOne = [departure:AirportBase, destination:AirportBase ]
    ...
    }
    

    AirportBase 必须是一个域。或者您可以使用类 Airport 进行映射

    class Flight extends FlightBase  {
        static hasOne = [departure:Airport, destination:Airport ]
    ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多