开发者

Class Foo contains objects Bar, Foo table has no pointer to Bar object, Bar object has FK to Foo.

开发者 https://www.devze.com 2023-03-21 06:27 出处:网络
I hope I can explain this problem clearly. I have a class Foo which contains 2 objects of type Bar.I have two mapping files, one for Foo and one for Bar.

I hope I can explain this problem clearly.

I have a class Foo which contains 2 objects of type Bar. I have two mapping files, one for Foo and one for Bar.

The database tables represent a Foo object, and a Bar object. The Foo db table has no reference to the Bars it owns. The Bar table has a FK reference to the Foo object they belong to.

Ideally, my Hibernate mapping file will be set up such that I save a Foo object, and it cascade saves/updates/deletes.

I am fairly new to Hibernate, and have researched differ开发者_C百科ent mapping options, but none seem to fit this scenario. Any advice is much appreciated, and I will remain available to answer any questions.

Thank you very much


What you're describing sounds perfectly normal, unless you mean that a Foo has two separate references to two different bars. Do you mean that, or that Foo owns a collection of Bars? The latter is more typical, but both are quite doable with Hibernate. It sounds like you're using XML mapping files, so everything you'll need is in the Hibernate reference.

Check out the tutorial in Chapter 1 first, of course, to get a feel for how to set up Hibernate. Chapter 3 is where you'll find all the configuration options. You don't need to read it all. Just know that's where to go to figure out how to configure Hibernate the way you want--that doesn't include mapping your objects, just everything else. Then you'll want to check out Chapter 5 to see how to map your basic properties, like Strings, ints, and Dates. It also has the basics of mapping "single-ended" relationships to other objects, such as if Foo has a private Bar bar;. There's a lot more detail on that in Chapter 8 if Chapter 5 doesn't get you there. Finally, if you need it, visit Chapter 7 to learn about mapping collections of objects.

If you start getting into that stuff and feel lost, glance over Chapter 6 and then start reading Chapter 11 to understand what Hibernate really does and how it works. There are also a few chapters dedicated to examples of mapping objects, starting with Chapter 24. Cascading is covered in that chapter, btw.

See where that gets you. If you get stuck on a particular error or a detail of mapping or configuration, ask another question and tell us exactly what problem you're having.

0

精彩评论

暂无评论...
验证码 换一张
取 消