`
dellsoft
  • 浏览: 110659 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

grails assgined the id

阅读更多

 

使用hibernate 的自定义主键

 

import org.hibernate.SessionFactory

class PersonController {
    SessionFactory sessionFactory 
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ personInstanceList: Person.list( params ), personInstanceTotal: Person.count() ]
    }

    def show = {
        def personInstance = Person.findById( params.id.toString() )

        if(!personInstance) {
            flash.message = "Person not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ personInstance : personInstance ] }
    }

    def delete = {
        def personInstance = Person.get( params.id )
        if(personInstance) {
            try {
                personInstance.delete()
                flash.message = "Person ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Person ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Person not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def personInstance = Person.get( params.id )

        if(!personInstance) {
            flash.message = "Person not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ personInstance : personInstance ]
        }
    }

    def update = {
        def personInstance = Person.get( params.id )
        if(personInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(personInstance.version > version) {
                    
                    personInstance.errors.rejectValue("version", "person.optimistic.locking.failure", "Another user has updated this Person while you were editing.")
                    render(view:'edit',model:[personInstance:personInstance])
                    return
                }
            }
            personInstance.properties = params
            if(!personInstance.hasErrors() && personInstance.save()) {
                flash.message = "Person ${params.id} updated"
                redirect(action:show,id:personInstance.id)
            }
            else {
                render(view:'edit',model:[personInstance:personInstance])
            }
        }
        else {
            flash.message = "Person not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def personInstance = new Person()
        personInstance.properties = params
        return ['personInstance':personInstance]
    }

    def save = {
        def session = sessionFactory.getCurrentSession()
        def tx = session.beginTransaction()
      
        def personInstance = new Person(params)
        Psnbbm psnbbm =  Psnbbm.findBySPrefix("CKNM")
        Long curId = psnbbm.curValue
        int nmLength = psnbbm.nmLength
        personInstance.id  =  (curId+1).toString().padLeft(9,"0")
        if(!personInstance.hasErrors() && personInstance.save()) {
            psnbbm.curValue = curId+1
            psnbbm.save()
            tx.commit()
            flash.message = "Person ${personInstance.id} created"
            redirect(action:show,id:personInstance.id)

        }
        else {
            tx.rollback()
            render(view:'create',model:[personInstance:personInstance])

        }
    }
}

 

 

   def session = sessionFactory.getCurrentSession()
        def tx = session.beginTransaction()
      
        def personInstance = new Person(params)
        Psnbbm psnbbm =  Psnbbm.findBySPrefix("CKNM")
        Long curId = psnbbm.curValue
        int nmLength = psnbbm.nmLength
        personInstance.id  =  (curId+1).toString().padLeft(9,"0")
        if(!personInstance.hasErrors() && personInstance.save()) {
            psnbbm.curValue = curId+1
            psnbbm.save()
            tx.commit()
            flash.message = "Person ${personInstance.id} created"
            redirect(action:show,id:personInstance.id)

        }
自定义的编码表

class Psnbbm {
    String sPrefix
    Long curValue
    int nmLength
    String description
    
    static constraints = {
      
    }
}

class Person {
        String id
        String foo

        static mapping = {
                id column: "id", generator: "assigned"
                version false
        }

    def beforeInsert = {

    }
}
方式二、
class Pwcity implements Serializable{
  String  id
  String  fchs
  String  feng
  String  fzip
  String  faircode
  String  ftype
  Long    forder = 0
  String  fctry
  Date    dateCreated
  Date    lastUpdated
  static constraints = {
//    fid(nullable:false,uinque:true,size:0..10)
    fchs(nullable:false,size:0..20)
    feng(nullable:false,size:0..40)
    fzip(nullable:false,size:0..10)
    faircode(nullable:false,size:0..10)
    ftype(nullable:false,size:0..8)
    fctry(nullable:false,size:0..20)
  }
  static mapping = {
    autoTimestamp false
    table 'PWCITY'
    version false
//    id composite :['fid']
    id     column: 'CITY_ID' ,sqlType:'varchar(10)',generator:'assigned',unique:'true'
    fchs    column: 'CITY_ZHS'
    feng    column: 'CITY_ENG'
    fzip    column: 'CITY_ZIP'
    faircode column: 'CITY_AIRCODE'
    ftype column: 'CITY_TYPE'
    forder column: 'CITY_ORDER'
    fctry column: 'CITY_CTRY'
    dateCreated column: 'YGZD_CRTIME'
    lastUpdated column: 'YGZD_UPTIME'
  }

  public void setId(String s) {
    id = s.padLeft(10,"0")
  }

  public String getId() {
    return id
  }


  def beforeInsert() {
    dateCreated = new Date()
    lastUpdated = new Date()
  }

  def beforeUpdate() {
    lastUpdated = new Date()
  }
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics