开发者

hibernate - component mapping vs custom value type

Please tell me wha开发者_如何学Ct is the difference between component mapping - custom value type and when to use it?

Thank you.


A value type maps on to a single database column. A custom value type is usually something that can be represented as a single column, but which Hibernate doesn't recognise (e.g. a JodaTime DateTime). With custom value types, you have to provide all the logic for converting to and from the column representation.

A component is more like a normal hibernate-mapped class, except the fields of the component are mapped on to columns of the parent class's table. Each field of a component type is usually a value type. You use components when you have a java class represented as multiple columns, but which shouldn't go in their own table.


Hibernate Reference

Well you can use them for similar stuff but in general:

component mapping is for mapping part directly to columns. Say you have a Name that is a class that is part of one of your Hibernate Entities (Containers). Hibernate doesn't recognized Name because it isn't mapped, but you can use the component tags to 'break down' the name object and map the parts to the Entity table.

Now, what is confusing is that you could make a custom value type called Name. But, what you are doing in this case is creating a value just like an Int or String that Hibernate recognizes because you use a special Interface to create it. A good idea of where this would be useful (from the Hibernate docs) is for a money value. You can create a value type that tells Hibernate how to deal with this money object you are passing around.

You will most likely be using component mappings more than custom value types. I believe in our app we just used custom types for Joda Time since Calendar sucks hehe.


Components are groups of columns mapped as objects which makes them reusable. They are easy to implement. Custom value types are a little bit harder to implement. You have to implement certain hibernate interfaces. On most cases I believe components would be enough. We used custom types for mapping GIS values, which required some parsing ...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜