Problem with mapping with recursive relation to itself in MyBatis Scala 1.0.2

267 views
Skip to first unread message

Anwar

unread,
Feb 3, 2014, 12:36:24 PM2/3/14
to mybati...@googlegroups.com
Hello, I have stuck on creating mapping on table with recursive relation to itself in MyBatis Scala 1.0.2

Table definition(PostgreSQL syntax)

CREATE TABLE T_USER
(
  ID                               BIGINT NOT NULL,
  LAST_MODIFIED_BY  BIGINT NOT NULL,
  LAST_MODIFIED        TIMESTAMP(3) WITHOUT TIME ZONE NOT NULL,
  LOGIN                        CHARACTER   VARYING(100) NOT NULL,
  PASSWORD               CHARACTER VARYING(100),

  CONSTRAINT T_USER_PK PRIMARY KEY (ID),
  CONSTRAINT T_USER_REF1 FOREIGN KEY (LAST_MODIFIED_BY) REFERENCES T_USER (ID) ON UPDATE CASCADE ON DELETE RESTRICT--references to itself
);

INSERT INTO T_USER VALUES
(1, 1, NOW(), 'ADMIN', '111'),
(2, 1, NOW(), 'USER', '222'),
(3, 2, NOW(), 'MANAGER', '333');

User.scala

import java.sql.{Timestamp}

class User {
  var id : Long = _
  var lastModified : Timestamp = _
  var lastModifiedBy : User = _
  var login : String = _
  var password : String = _

  def this(id: Long, login: String, password: String) {
    this()
    this.id = id
    this.login = login
    this.password = password
  }

  override def toString = s"User[id: $id, lastModified: $lastModified, lastModifiedBy: ${if(lastModified == null) "none" else lastModifiedBy.login}, login: $login, password: $password]"
  
}
 
DAO.scala

import org.mybatis.scala.mapping._
import java.sql.Timestamp
import org.mybatis.scala.mapping.Binding._
import org.mybatis.scala.config.Configuration
import org.apache.ibatis.`type`.SqlTimestampTypeHandler

object DAO {

  private val UserResultMap = new ResultMap[User] {
    id(property="id", column="id")
    result(property="lastModified", column="last_modified", jdbcType = JdbcType.TIMESTAMP, typeHandler = T[SqlTimestampTypeHandler])
    result(property="login", column="login")
    result(property="password", column="password")
    association[User](property="lastModifiedBy", column = "last_modified_by", select = getUserById, jdbcType = JdbcType.BIGINT)

  }

  val getUserById = new SelectOneBy[Long, User] {
    def xsql =
      <xsql>
        select
          id,
          last_modified_by,
          last_modified,
          login,
          password
        from
          t_user
        where
          id = {?("value", jdbcType = JdbcType.BIGINT)}
      </xsql>
  }
  val getUserByLogin = new SelectOneBy[String, User] {
    def xsql =
      <xsql>
        select
          id,
          last_modified_by,
          last_modified,
          login,
          password
        from
          t_user
        where
          login = {?("value", jdbcType = JdbcType.VARCHAR)}
      </xsql>
  }
  val getAllUsers = new SelectList[User] {
    def xsql =
      <xsql>
        select
          id,
          last_modified_by,
          last_modified,
          login,
          password
        from
          t_user
      </xsql>
  }

  private val config = Configuration("mybatis.xml")
  config += getUserById
  config += getUserByLogin
  config += getAllUsers

  val sessionManager = config.createPersistenceContext


}

Test 

object MyApp extends App{

  val u = new User(1, "John", "222")
  println(u.toString)

  DAO.sessionManager.transaction{implicit session =>

    val byLogin = DAO.getUserByLogin("ADMIN")
    println(byLogin.map(_.toString).getOrElse("Not found by login"))

    val byId = DAO.getUserById(2)
    println(byId.map(_.toString).getOrElse("Not found by id"))

    for(user <- DAO.getAllUsers())
      println(user.toString)

  }

}


output

User[id: 1, lastModified: null, lastModifiedBy: none, login: John, password: 222]
User[id: 1, lastModified: null, lastModifiedBy: none, login: ADMIN, password: 111]
User[id: 2, lastModified: null, lastModifiedBy: none, login: USER, password: 222]
User[id: 1, lastModified: null, lastModifiedBy: none, login: ADMIN, password: 111]
User[id: 2, lastModified: null, lastModifiedBy: none, login: USER, password: 222]
User[id: 3, lastModified: null, lastModifiedBy: none, login: MANAGER, password: 333]

But I can't understand what is wrong with the mapping?

Frank Martínez

unread,
Feb 4, 2014, 9:17:21 AM2/4/14
to mybati...@googlegroups.com
You are not using the resultMap.

put

resultMap = UserResultMap

before def xsql = ....




--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Frank D. Martínez M.

Anwar

unread,
Mar 25, 2014, 9:12:19 AM3/25/14
to mybati...@googlegroups.com
Hello thanks for response. But I've go exception

Exception in thread "main" java.lang.ExceptionInInitializerError
at test.Test$delayedInit$body.apply(Test.scala:11)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.collection.immutable.List.foreach(List.scala:318)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
at scala.App$class.main(App.scala:71)
at test.Test$.main(Test.scala:6)
at test.Test.main(Test.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.IllegalStateException: No typehandler found for property lastModifiedBy
at org.apache.ibatis.mapping.ResultMapping$Builder.validate(ResultMapping.java:147)
at org.apache.ibatis.mapping.ResultMapping$Builder.build(ResultMapping.java:136)
at org.apache.ibatis.builder.MapperBuilderAssistant.assembleResultMapping(MapperBuilderAssistant.java:426)
at org.apache.ibatis.builder.MapperBuilderAssistant.buildResultMapping(MapperBuilderAssistant.java:222)
at org.apache.ibatis.builder.MapperBuilderAssistant.buildResultMapping(MapperBuilderAssistant.java:504)
at org.mybatis.scala.config.ConfigurationSpace$$anonfun$org$mybatis$scala$config$ConfigurationSpace$$addResultMap$1.apply(ConfigurationSpace.scala:107)
at org.mybatis.scala.config.ConfigurationSpace$$anonfun$org$mybatis$scala$config$ConfigurationSpace$$addResultMap$1.apply(ConfigurationSpace.scala:103)
at scala.collection.immutable.List.foreach(List.scala:318)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
at scala.collection.mutable.ListBuffer.foreach(ListBuffer.scala:45)
at org.mybatis.scala.config.ConfigurationSpace.org$mybatis$scala$config$ConfigurationSpace$$addResultMap(ConfigurationSpace.scala:103)
at org.mybatis.scala.config.ConfigurationSpace.org$mybatis$scala$config$ConfigurationSpace$$addStatement(ConfigurationSpace.scala:166)
at org.mybatis.scala.config.ConfigurationSpace.$plus$eq(ConfigurationSpace.scala:46)
at org.mybatis.scala.config.Configuration.$plus$eq(Configuration.scala:61)
at test.DAO$.<init>(DAO.scala:69)
at test.DAO$.<clinit>(DAO.scala)
... 15 more



вторник, 4 февраля 2014 г., 20:17:21 UTC+6 пользователь Frank Martinez написал:
Reply all
Reply to author
Forward
0 new messages