Please tell me what 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With