Hibernate – @ASSOCIATIONOVERRIDES @ATTRIBUTEOVERRIDES @EMBEDDABLE @EMBEDDED

With hibernate we can design domain class with many feature, now I will create some example to understanding about Embeddable Annotation, in final i will create some example to custom that Embeddable using Attribute overrides.

domain-design

I have Entity class with name Person, that class embed all properties from Address class, so all property in Address will be included into Person class. Address class has relation many to one with City class

City.java

package com.meng.test.hibernate.domain;

import javax.persistence.*;

@Entity
@Table(name = "CITY")
public class City {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "GEN_CITY_ID")
	@SequenceGenerator(name = "GEN_CITY_ID", sequenceName = "SEQ_CITY_ID", initialValue = 1, allocationSize = 1)
	@Column(name = "ID")
	private Long id;
	@Column(name = "NAME")
	private String name;

	// getter and setter

}

City class will create table with structure like below

mysql> desc city;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| ID    | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| NAME  | varchar(255) | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

Address.java

package com.meng.test.hibernate.domain;

import javax.persistence.*;

@Embeddable
public class Address {

	@Column(name = "STREET")
	private String street;
	@Column(name = "POSTAL_CODE")
	private String postalCode;
	@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinColumn(name = "CITY_ID")
	private City city;

	// getter and  setter

}

Person.java

package com.meng.test.hibernate.domain;

import javax.persistence.*;

@Entity
@Table(name = "PERSON")
public class Person {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "GEN_PERSON_ID")
	@SequenceGenerator(name = "GEN_PERSON_ID", sequenceName = "SEQ_PERSON_ID", initialValue = 1, allocationSize = 1)
	@Column(name = "ID")
	private Long id;

	@Column(name = "NAME")
	private String name;

	@Embedded
	private Address address;

	// getter and setter

}

Person class will generate table with structure include Address properties like below

mysql> desc person;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| ID          | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| POSTAL_CODE | varchar(255) | YES  |     | NULL    |                |
| STREET      | varchar(255) | YES  |     | NULL    |                |
| NAME        | varchar(255) | YES  |     | NULL    |                |
| CITY_ID     | bigint(20)   | YES  | MUL | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

As we look in person table, all address class properties included into person class.

And now how we can custom column name

Employee.java

package com.meng.test.hibernate.domain;

import javax.persistence.*;

@Entity
@Table(name = "EMPLOYEE")
public class Employee {
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "GEN_EMPLOYEE_ID")
	@SequenceGenerator(name = "GEN_EMPLOYEE_ID", sequenceName = "SEQ_EMPLOYEE_ID", initialValue = 1, allocationSize = 1)
	@Column(name = "ID")
	private Long id;
	
	@Column (name = "NAME")
	private String name;
	
	@Embedded
	@AssociationOverrides({ 
        @AssociationOverride(name = "city", joinColumns =  @JoinColumn(name = "E_CITY_ID")) })
	@AttributeOverrides({
			@AttributeOverride(name = "street", column = @Column(name = "E_STREET")),
			@AttributeOverride(name = "postalCode", column = @Column(name = "E_POSTAL_CODE"))})
	private Address address;

	// getter and setter

}
mysql> desc employee;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| ID            | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| E_POSTAL_CODE | varchar(255) | YES  |     | NULL    |                |
| E_STREET      | varchar(255) | YES  |     | NULL    |                |
| NAME          | varchar(255) | YES  |     | NULL    |                |
| E_CITY_ID     | bigint(20)   | YES  | MUL | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

as we look in employee table, properties in Address class override with new name

Share and Enjoy !

0Shares
0 0