双向在哪里? grails 一对一,双向测试(继续)

发布于 2024-10-09 03:32:27 字数 1122 浏览 5 评论 0原文


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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

川水往事 2024-10-16 03:32:27

我已经找到了如何让它工作的解决方案,但仍然无法理解为什么它在没有“刷新”的情况下不起作用,见下文:



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)

     def p = person.save()

     assertNotNull p

     person.refresh() //load the object again from the database so all the changes made to object will be reverted
     //person = Person.get(p.id)   // BUT this also gets the object from db ...?

     def bookId = person.getBook().id
     assertNotNull bookId

     def thatBook = Book.get(bookId)
     assertNotNull(thatBook.person)
  }

 

所以,在这里你可以看到我使用“刷新”让它工作,但为什么它会这样做如果没有“刷新”,则无法工作,但在“刷新”后使用以下行 - 这一行:
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:



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)

     def p = person.save()

     assertNotNull p

     person.refresh() //load the object again from the database so all the changes made to object will be reverted
     //person = Person.get(p.id)   // BUT this also gets the object from db ...?

     def bookId = person.getBook().id
     assertNotNull bookId

     def thatBook = Book.get(bookId)
     assertNotNull(thatBook.person)
  }

 

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?

别想她 2024-10-16 03:32:27

您的问题可能是由 Hibernate 的工作方式引起的。 Grails 在底层使用了 Hibernate。

即使您调用“save”,对象 person 也可能(并且通常)不会保存在数据库中。这是因为 Hibernate 被编程为优化查询,因此它经常等待在 Hibernate 会话结束时执行所有查询。

这意味着如果您不调用“刷新”,书人关系(person.setBook)仍然在内存中,但不会保存在数据库中。因此,您无法从 book 获取 book.person

要强制保存,您可以像前面的答案一样使用“refresh”,或者使用flush:true。

我仍然没有尝试,但您很可能会通过以下方式产生所需的结果:

person.save(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 from book.

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:

person.save(flush:true)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文