2016-09-16 132 views
-2

我在我的数据库中有一列名为invoice_number的列。从Grails中的数据库检索值

class Customer { 
    String name 
    int invoiceNumber 

    static constraints = { 
     name(blank: false) 
     invoiceNumber(unique: true) 
    } 
} 

index.gsp文件中的invoice_number没有字段。

<g:form controller="customer"> 
    Name:<br> 
    <g:textField name="name"></g:textField><br> 
    <g:actionSubmit value="Submit" action="Save"></g:actionSubmit><br> 
</g:form> 

我要生成一个发票号码,并用5。例如有差别,当第一客户提交表格,发票数可以为105。当第二客户提交的形式中,发票生成增加它数字应该是110.它们应该保存在数据库中,并且它们必须是唯一的。 然后,我想从提交表单的客户的数据库检索发票号码,然后将该发票号码传递给另一个gsp文件。

我该怎么办?

+1

嗯...所以客户只能有1张发票吗?自从?是否有真正的商业理由将发票数量增加5? 106..109会发生什么? – railsdog

+0

正如@railsdog所指出的那样,为什么要将发票号码增加5?您可以简单地将其增加1,它仍然可以是唯一的,对吗?同样从你的域名类看来,只有一张发票将被映射给客户。这是业务逻辑吗? –

+0

这里的一切似乎只是错误的。这就是为什么你没有收到任何好的答案。正如railsdog提到的 - 1个用户和1个发票似乎很奇怪。您应该创建发票作为课程,并向用户添加一对多(如果您想要多于一张发票)。并且该数字可以是域名函数'afterInsert',其变为'invoiceNumber = id * 5 + 100'或类似的东西。 (但是只有当您有发票作为单独的课程时才有效。 –

回答

0

您需要添加生成的逻辑/递增invoiceNumbercontroller's action到你在form submit给人打电话。

0

可能让你开始在你的路上(但我仍然没有得到每个客户部分的1张发票,或增加5)。是的,正如Abhinandan提到的那样,你可以在控制器中放置ID创建的逻辑,但是更多的可重用路线可能是创建一个自定义密钥生成器,并指出你的类使用这个生成器来记录ID。

假设我们有:

package tester2 
class Custo { 
    String id // string just to show it can be anything 
    String name 

    // tell GORM about your id attribute, and setup your 'id' column 
    // to use a custom id generator function 
    static mapping = { 
     id column:"id", generator:"tester2.CustomIdGenerator" 
    } 
} 

然后将src /常规/ tester2/CustomIdGenerator.groovy

package tester2 
import org.hibernate.id.IdentifierGenerator 
import org.hibernate.engine.spi.SessionImplementor 

class CustomIdGenerator implements IdentifierGenerator { 
    public synchronized Serializable generate(SessionImplementor session, Object obj) { 
     // here's where you would have to access some persistent store to 
     // remember your last generated ID, fetch the last value, add 5, 
     // and store that new value to be ready for the next call 
     // -- don't know what DBMS you intend to use, but some have 
     // sequence support that will let you tinker with the starting 
     // point and increment. Maybe it's a simple as setting up a 
     // customized sequence and asking for nextVal() 
     // 
     // for this example, I just use a random UUID 
     return UUID.randomUUID().toString() 
    } 
}