The forum address has changed, you have been automatically redirected. Please update any bookmarks to use the new URL.

Subscribe

scala: error updating object (unique index)

You are viewing a single message from this topic. View all messages.

  1. 2009-07-28 20:36:55 UTC
    Hi

    I'm trying out neodatis with scala, and I've got a problem. When trying to update an object that has a unique index on one of it's fields (not the one I'm changing) I get an "Duplicate key on index ..." error.

    Here is the code I'm trying:

    ========================

    import org.neodatis.odb.{ODB, ODBFactory}
    import org.neodatis.odb.core.query.IQuery
    import org.neodatis.odb.impl.core.query.criteria.CriteriaQuery
    import org.neodatis.odb.core.query.criteria.Where
    import java.io.File

    object Test {

    case class Login(login: String, private var password: String, var firstName: String, var lastName: String, var email: String) {
    if (null == login || login.isEmpty) throw new IllegalArgumentException("login is empty!")
    if (null == password || password.isEmpty) throw new IllegalArgumentException("password is empty")

    def setPassword(password: String) = {
    if (null == password || password.isEmpty) throw new IllegalArgumentException
    this.password = password
    }

    def getPassword = password
    }

    def main(args: Array[String]) {
    val server = ODBFactory.openServer(9876)
    val dbname = "ec2sif"
    try {
    server.addBase(dbname,"target/data.db")
    val loginIndex = "login_index"

    def connect() = server.openClient(dbname)

    // set indexes and unique constraint
    var odb = connect
    if (!odb.getClassRepresentation(classOf[Login]).existIndex(loginIndex)) {
    val fieldIndex = Array("login")
    odb.getClassRepresentation(classOf[Login]).addUniqueIndexOn(loginIndex, fieldIndex, true)
    }
    odb.close

    // let's create and persist our login
    val l = Login("login","password",null,null,null)
    odb = connect
    odb.store(l)
    odb.close

    // let's modify this object
    val query: IQuery = new CriteriaQuery(classOf[Login], Where.equal("login", "login"))
    odb = connect
    val objects = odb.getObjects(query)
    val persistedLogin: Login = objects.getFirst
    if (l != persistedLogin) throw new RuntimeException("logins doesn't match") // sanity check
    persistedLogin.firstName = "MyName"
    odb.store(persistedLogin)
    odb.close
    } finally {
    // cleanup
    server.close
    (new File(dbname)).delete
    }
    }
    }

    ========================

    Is it a bug or am I doing something wrong?

    I'm using latest version (1.9.3).

    Thanks in advance

    Haim Ashkenazi
< Previous | 1 | Next >

Add a Reply

This forum does not allow anonymous participation.

Log in to add a reply. Not registered? Create an account to participate and receive email updates when replies are posted to this topic.