Entity definition
Goal
Learn how to describe an entity in terms of Java Persistence API
Subject
You need to describe the Company entity that is mapped onto two database tables: Company and CompanyDetail.
Here are the tables DDLs:
CREATE TABLE Company (
Company_id INT PRIMARY KEY,
Company_name VARCHAR(50)
);
CREATE TABLE CompanyDetail (
Company_id INT PRIMARY KEY,
CompanyDetail_address VARCHAR(100)
);
Attributes of Company entity are mapped to tables’ fields as follows:
o Company.id Company.Company_id
o Company.name Company.Company_name
o Company.address CompanyDetail.CompanyDetail_address
Description
-
Open module jpa-lab-01
-
Open class edu.jpa.entity.Company
-
Specify class-level annotation @Entity. This lets JPA runtime to know that this particular class should be treated as an entity.
-
Specify class-level annotation @Table with the name of primary table “Country”.
-
Specify class-level annotation @SecondaryTable with the name of secondary table “CompanyDetail”. The parameter pkJoinColumns of @SecondaryTable annotation specifies how to join primary and secondary table when building an entity: it should be @PrimaryKeyJoinColumn(name = "Company_id", referencedColumnName = "Company_id").
-
Define Country entity fields: id (of the type Integer), name (of the type String) and address (of the type String).
-
Specify field-level annotation @Column for each field with information which field is to be mapped on this particular field. Example: @Column(name="Company_id", table="Company")
-
Specify field-level annotation @Id for the id field.
-
Open and run the class edu.jpa.Launcher. There should be no errors if entity is defined correctly.
-
Open database DB_LAB_01 using MySQL Workbench and look on the created database objects.
In Workbench: right‑click db_lab_01 → Set as Default Schema (or run USE DB_LAB_01;).
Refresh schemas (right‑click Schemas → Refresh).
Run:
SHOW TABLES FROM DB_LAB_01;
DESCRIBE DB_LAB_01.Company;
DESCRIBE DB_LAB_01.CompanyDetail;
SELECT * FROM DB_LAB_01.Company;
SELECT * FROM DB_LAB_01.CompanyDetail;
Tip
Ensure Workbench is connected to the same server/port as the app (localhost:3306).
You can check with SELECT DATABASE(), @@port;.