Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate - component mapping vs custom value type

Tags:

java

hibernate

Please tell me what is the difference between component mapping - custom value type and when to use it?

Thank you.

like image 737
robinmag Avatar asked Feb 28 '23 20:02

robinmag


2 Answers

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.

like image 90
skaffman Avatar answered Mar 06 '23 22:03

skaffman


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.

like image 35
Arthur Thomas Avatar answered Mar 06 '23 22:03

Arthur Thomas