双向在哪里? grails 一对一,双向测试(继续)
class Book {
String title
Date releaseDate
String ISBN
static belongsTo = [person:Person] // it makes relationship bi-directional regarding the grails-docs
}
class Person {
Book book; // it will create person.book_id
String name
Integer age
Date lastVisit
static constraints = {
book unique: true // "one-to-one". Without that = "Many-to-one".
}
}
有一个测试可以测试它是否是真正的双向。据我了解。
public void testBidirectional() {
def person = new Person(name:"person_c1", age: 99, lastVisit: new Date())
def book = new Book(
title:"somebook_c1",
ISBN: "somebook_c1",
releaseDate: new Date()
)
person.setBook (book)
assertNotNull(person.save())
def bookId = person.getBook().id
Book thatBook = Book.get(bookId)
assertNotNull(thatBook.person) // NULL !!!
}
因此,我用书保存了一个人,然后我通过id从数据库中获取了该书。然后从那本书中我尝试找回哪本书应该引用的人(因为它应该是双向的,对吧?)。最终我得到了 null 而不是 person 的实例。
问题是:如何使该测试有效?
class Book {
String title
Date releaseDate
String ISBN
static belongsTo = [person:Person] // it makes relationship bi-directional regarding the grails-docs
}
class Person {
Book book; // it will create person.book_id
String name
Integer age
Date lastVisit
static constraints = {
book unique: true // "one-to-one". Without that = "Many-to-one".
}
}
There is a test which test if it is real bidirectional or not. As i understand it.
public void testBidirectional() {
def person = new Person(name:"person_c1", age: 99, lastVisit: new Date())
def book = new Book(
title:"somebook_c1",
ISBN: "somebook_c1",
releaseDate: new Date()
)
person.setBook (book)
assertNotNull(person.save())
def bookId = person.getBook().id
Book thatBook = Book.get(bookId)
assertNotNull(thatBook.person) // NULL !!!
}
So, i save a person with a book, and then i got that book from db by id. Then from that book i try to get back the person which book should refer to (because it should be bidirectional, right?). Eventually i got null instead of an instance of the person.
The questing is: how to make that test working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经找到了如何让它工作的解决方案,但仍然无法理解为什么它在没有“刷新”的情况下不起作用,见下文:
所以,在这里你可以看到我使用“刷新”让它工作,但为什么它会这样做如果没有“刷新”,则无法工作,但在“刷新”后使用以下行 - 这一行:
person = Person.get(p.id) // 但这也从数据库获取对象...?
如果我只想通过 id 从数据库中获取对象,那么它不会是双向的吗?
i have found the solution how to get it working, but still can not understand why it does not work without 'refresh', see below:
So, here as you can see i use 'refresh' to get it working, but why it does not work without 'refresh' but with the following line after 'refresh' - this one:
person = Person.get(p.id) // BUT this also gets the object from db ...?
If i just want to get object from database by id, then it would be without bidirectional?
您的问题可能是由 Hibernate 的工作方式引起的。 Grails 在底层使用了 Hibernate。
即使您调用“save”,对象 person 也可能(并且通常)不会保存在数据库中。这是因为 Hibernate 被编程为优化查询,因此它经常等待在 Hibernate 会话结束时执行所有查询。
这意味着如果您不调用“刷新”,书人关系(person.setBook)仍然在内存中,但不会保存在数据库中。因此,您无法从
book
获取book.person
。要强制保存,您可以像前面的答案一样使用“refresh”,或者使用flush:true。
我仍然没有尝试,但您很可能会通过以下方式产生所需的结果:
Your problem is probably caused by the way that Hibernate works. Grails used Hibernate under the hood.
Even when you call "save", the object person may (and usually) not saved in database. That's because Hibernate is programmed to optimize the query, so it often waits to perform all query at then end of the Hibernate session.
That means if you don't call "refresh", the book-person relation (person.setBook) is still in memory, but not saved in database. Hence you can't get the
book.person
frombook
.To enforce the save, you can use "refresh" like the previous answer, or use flush:true.
I still not try, but it's very likely that you will produce desired results with: