在使用DataSource的Grails中,我试图插入数据,但只有id字段才能保存另一个字段

时间:2014-12-08 05:41:41

标签: grails

我正在尝试将数据插入到Oracle数据库中,并且只有id字段被插入,其余的字段值为空值。

客户域类:

package com.app

class Customer {

   def customerId

   def customerName

   def userName

   def password

   def email

   def contactNum;

   //def Address address;

   static mapping = {
      table name:"AACUSTOMER"
      version false
      customerId column:'ID', generator: 'sequence', params:[sequence:'AACUSTOMER_ID_SEQ']
      customerName column:'CUSTOMER_NAME'
      userName column:'USERNAME'
      password column:'PASSWORD'
      email column:'EMAIL'
      contactNum column:'CONTACT_NUM'
   }
}

customerController:

package com.app

class CustomerController {

   def index() { 
      render(view:"addCustomer")
   }

   def addCustomer() {

      def name = params.name
      def username = params.username
      def password = params.pwd
      def repassword = params.rpwd
      def email = params.email
      def contactNum = params.contactnum
      if(password==repassword) {
         Customer customer = new Customer();
         customer.setCustomerName(name)
         customer.setUserName(username)
         customer.setPassword(password)
         customer.setEmail(email)
         customer.setContactNum(contactNum)
         println "${customer.getCustomerName()}"
         customer.save(flush: true);
      }
      else {
         render(message:"password should match")
      }
   }
}

DataSource.groovy中:

dataSource {
   pooled = true
   driverClassName = "oracle.jdbc.driver.OracleDriver"
   username = "wmdev"
   password = "wmdev"
   url = "jdbc:oracle:thin:@172.20.1.196:1521/LCLWMSDEV01"
}

hibernate {
   cache.use_second_level_cache = true
   cache.use_query_cache = false
   cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
//    cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegiossnFactory' // Hibernate 
}

// environment specific settings

environments {
   development {
      dataSource {
         dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
         url = "jdbc:oracle:thin:@172.20.1.196:1521/LCLWMSDEV01"
      }
   }

   test {
      dataSource {
         dbCreate = "create-drop"
         url = "jdbc:oracle:thin:@172.20.1.196:1521/LCLWMSDEV01"
      }
   }

   production {
      dataSource {
         dbCreate = "update"
         url = "jdbc:oracle:thin:@172.20.1.196:1521/LCLWMSDEV01"
         properties {
            maxActive = -1
            minEvictableIdleTimeMillis=1800000
            timeBetweenEvictionRunsMillis=1800000
            numTestsPerEvictionRun=3
            testOnBorrow=true
            testWhileIdle=true
            testOnReturn=false
            validationQuery="SELECT 1"
            jdbcInterceptors="ConnectionState"
         }
      }
   }
}

1 个答案:

答案 0 :(得分:1)

你也是如此" groovy"。必须键入GORM域类属性,并且具有GORM和Hibernate知道如何持久化的标准类型,或者您需要提供CustomType实现来为其执行持久性工作。如果你没有输入类属性,它会被忽略,因为没有办法知道userName可能是一个字符串(但它也可能是主键,需要与其他属性区别对待,或者customerId可能是Customer域类和客户表中的Long和外键,或者是String(仍然可能是PK / FK)。您需要提供一些信息来获取内容启动。

相关问题