http://websystique.com/springmvc/spring-mvc-4-and-spring-security-4-integration-example/
Arquivo da categoria: Hibernate
Spring 4 with Hibernate 4
Spring 4 + Hibernate 4 + MySQL+ Maven Integration example (Annotations+XML)
Configurando o JBoss + Spring + JPA (Hibernate) + JTA
AOP na prática: implementando um aspecto de auditoria
Hibernate – Chapter 15. Envers
JPA – Mapeamento com chave composta
Municipio
@Entity
@Table(name = "municipio")
public class Municipio {
@EmbeddedId
private MunicipioPK id;
@Column(name = "DSC_MUNICIPIO")
private String nome;
@Column(name = "NUM_CEP")
private Long cep;
@ManyToOne
@JoinColumn(name = "COD_UF", referencedColumnName = "COD_UF", insertable = false, updatable = false)
private UnidadeFederacao uf;
}
MunicipioPK
@Embeddable
public class MunicipioPK implements Serializable {
@Column(name = "COD_MUNICIPIO")
private Integer idMunicipio;
@Column(name = "COD_UF")
private Integer idUnidadeFederacao;
}
UnidadeFederativa
@Entity
@Table(name = "unidade_federacao")
public class UnidadeFederacao {
@Id
@Column(name = "cod_uf")
private Integer id;
@Column(name = "dsc_uf")
private String nome;
@Column(name = "nom_sigla_uf")
private String sigla;
@OneToMany(mappedBy = "uf", fetch = FetchType.LAZY)
private List municipios;
}
Montando uma aplicação completa em Java
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.
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
Dominando JavaServer Faces e Facelets Utilizando Spring 2.5, Hibernate e JPA
Nota do livro: 6.