In last article, we have discussed about component mapping in hibernate using JPA annotation. Strongly recommend to read this post.
Lets start the discussion about mapping of columns with the object attributes (member variables).
@Entity @Getter @Setter public class Person { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false) private String name; @Embedded private Address address; public Person() { } public Person(String name, Address address) { this.name = name; this.address = address; } }
@Embeddable public class Address { private String street; private String city; private String zipcode; public Address() { } public Address(String street, String city, String zipcode) { this.street = street; this.city = city; this.zipcode = zipcode; } }
The table is automatic create by hibernate because of
hbm2ddl.auto=create-drop
properties set into hibernate.cfg.xml file. But
we have nowhere defined the name of the table and column of the table. Where did this "Person"
table come from and columns of this table?
We have not written the schema for it, no mapping file and not used @Table
JPA
annotations for mapping. Even we have not used @Column
annotation or mapping file
to map id attribute to id column of this table. Similarly for the rest of the
attributes. Hibernate automatically figure out about it. First hibernate uses the reasonable
default values for xml based mapping metadata and also for annotations based mapping metadata.
So when it finds a mapping information missing. Then it uses sensible default value for that.
When It doesn't find the @Table
annotation on entity. It is going to use the name
of entity as table name. So here name of the entity is "Person"
so it is mapped to
the name of the table i.e "Person".
Similarly, It doesn't find the @Column
annotation on an attribute. It is going to
use the attribute name as column name of the table.
Hibernate internally treats them like below:
@Entity @Table(name = "Person") @Getter @Setter public class Person { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Long id; @Column(name = "name", nullable = false) private String name; @Embedded private Address address; public Person() { } public Person(String name, Address address) { this.name = name; this.address = address; } }
@Embeddable public class Address { @Column(name = "street") private String street; @Column(name = "city") private String city; @Column(name = "zipcode") private String zipcode; public Address() { } public Address(String street, String city, String zipcode) { this.street = street; this.city = city; this.zipcode = zipcode; } }
When we run the application that time It create the DDl script using the mapping metadata of the person entity and execute the schema against the "helloworld" database.
create table Person (id bigint not null auto_increment, city varchar(255), street varchar(255), zipcode varchar(255), name varchar(255) not null, primary key (id)) engine=InnoDB
The name column of the Person table is having not null
constraint
that is coming because of @Column(nullable=false)
.
No comments :
Post a Comment