gorm

Golang的tag语法

可紊 提交于 2020-02-25 20:01:58
[TOC] Golang的tag语法 谢谢慕课网精英讲师"cap1537" 我们可以通过Tag来增强结构体的定义,Tag会带上一些meta信息,在本文中我们将通过几个例子来深入了解Tag的用法。 结构 Struct是由一组field组成,每个field包括了名字(可选)和字段类型 package main import "fmt" type T1 struct { f1 string } type T2 struct { T1 f2 int64 f3, f4 float64 } func main() { t := T2{T1{"foo"}, 1, 2, 3} fmt.Println(t.f1) // foo fmt.Println(t.T1.f1) // foo fmt.Println(t.f2) // 1 } field T1是一个嵌入型field, 它并没有字段名 。Field定义可以像f3和f4一样公用一个字段类型。 标签 有时候会在字段定义后面带上一个字符串(tag)。类似于如下 type T struct { f1 string "f one" f2 string f3 string `f three` f4, f5 int64 `f four and five` } 不管是raw string还是interpreted string都可以用来当tag。

Adding changes listeners in Grails' GORM

淺唱寂寞╮ 提交于 2020-02-02 13:38:29
问题 I'm new to Grails and I'm using Grails 2.0.1. I want to add a persistence event listener for changes in objects for a domain class, so I tried the code in Bootstrap.groovy as given in the user guide: def init = { applicationContext.addApplicationListener(new FooBarListener()) } And I get the following error message: ERROR context.GrailsContextLoader - Error executing bootstraps: No such property: applicationContext for class: BootStrap How can I get the applicacionContext property from inside

How to fetch records in grails by max date and group at same time

╄→гoц情女王★ 提交于 2020-02-02 02:20:15
问题 I have a table that looks like this: id name shade date_created ---- ----- ------- --------------- 1 Red bright 10-28-2012 2 Orange light 10-28-2012 3 Red <null> 10-24-2013 4 Orange light 10-24-2013 Desired Result: id name value date_created ---- ----- ------ --------- 3 Red <null> 10-24-2013 4 Orange light 10-24-2013 What can I do with GORM to get this result? In pure sql this is the query that gets me the desired result: SELECT t.name, t.shade, r.MaxTime FROM (SELECT name, MAX(date_created)

Grails plugin for DocumentDB

偶尔善良 提交于 2020-01-26 00:45:28
问题 We are kind of POC phase where we are trying out Azure. We are using Grails, which has nice support for MongoDb as backend (using GORM), but couldn't find similar support for DocumentDd. If we choose to go with DocumentDb, our choice would be to have a layer (may be kind of DAO layer) which uses documentDb client library and interacts with DocumentDb. Or we fall back to MongoDb, as its provided Azure. Any other choices we have with Grails-DocumentDb? 回答1: You should be able to use DocumentDB

Why does foreign key not get generated with GORM?

三世轮回 提交于 2020-01-25 10:13:47
问题 I am trying to create a foreign key on the Password table which must point to the id column inside the User table. But as I try the following, it does not work. The foreign key is not generated. It simply adds the column name user_id inside the password table. package schema import ( "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/mysql" ) type User struct { gorm.Model FirstName string `gorm:"not null"` LastName string `gorm:"not null"` Email string `gorm:"type:varchar(100);unique

How to write between clause for from & to dates for createCriteria in grails?

孤街醉人 提交于 2020-01-21 10:10:13
问题 I want to fetch results between 2 dates using following code. def c = TestCase.createCriteria() resultss = c.list { like("testStatus", "Dummy") and { between("testTime", date1, date2) } order('testified','desc') } Here I want to select From Date and To Date like below FROM DATE : '2014-11-07 12:14:03'(date1) TO DATE : '2015-01-09 08:14:12'(date2) I tried a lot but no luck to get it run. Can anybody please tell me how to do that? 回答1: the and there is odd; first of all and is the default

alternative to grails multicolumn unique constraint (optimistic inserts)

拈花ヽ惹草 提交于 2020-01-17 04:22:49
问题 I have a heavy used domain class Relationship which looks pretty similar to following one class Relationship { Element source Element destination Type type // other properties omitted static constraints = { type unique: ['source', 'destination'] } } I have a service creating new instances of this domain class which checks for existing instances and if found reuses them but I would like to implement sort of optimistic inserts with the unique constraint only in the database because GORM unique

Grails/Groovy: Modify a “query” closure at runtime

耗尽温柔 提交于 2020-01-16 04:58:08
问题 In a gorm domain class, I can do def q = { property1{ eq('attr', 0) } } MyDomainClass.list(q) How could I modify the closure 'q' (or create a new closure that would contain the restrictions that closure 'q' has) at runtime so for example I could add another property restriction? More details Actually my problem is how to create combined criteria in a Domain Class Hierarchy. class Parent{ int pAttr static def getCriteria(){ def dummyParentCriteria = { eq('pAttr', 0) } } } class Child extends

Grails Mapping LocalDateTime of Java 8 domain objects to Gorm

让人想犯罪 __ 提交于 2020-01-16 04:50:09
问题 Java 8 gives the java.time API. I am using a grails application, I want to use java.time for properties of my domain classes. I need to map for instance the LocalDateTime to the DATE/DATETIME on GORM which is built on top of Hibernate. How can I persist my mappings? I see a solution, that I can use jadira, and establish static mappings for my attributs. However is there another way? Please any help is appreciated. 回答1: You can use the user type mapping. See the docs. That should remove the

How to prevent from Grails not to delete child while deleting parent?

本小妞迷上赌 提交于 2020-01-15 02:47:24
问题 i have one to many relationship between author and books, one author has many books.. i have domain class like this Author.groovy class Author { String authorName; String authorAddress; String authorCNIC; static hasMany = [books:Book] static constraints = { books(nullable: true) } } Book.groovy class Book { String bookName; String Isbn; static belongsTo = [author:Author] static constraints = { author(nullable: true) } } now when i call this function def deleteauthor() { def id=params.id