我的数据库中有一列名为invoice_number。
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文件。
我该怎么做?
答案 0 :(得分:0)
您需要添加生成/递增 invoiceNumber
的{{1}}逻辑,以controller's action
为您致电{。}}。
答案 1 :(得分: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 / groovy / 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()
}
}