JPA Hibernate map fields from class within class to same table - java

I have a class 'Audit' that will hold the following details about an entity:
Time Created
Created by which application user
Time Updated
Updated by which application user
On the database, these fields are stored on the same table as the entity attributes. For example,
USER table:
CREATE TABLE USERS (
id BIGINT NOT NULL AUTO_INCREMENT,
display_name VARCHAR(30) NOT NULL,
active BOOLEAN NOT NULL DEFAULT FALSE,
created_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
created_user BIGINT NOT NULL DEFAULT 0,
updated_time TIMESTAMP NOT NULL DEFAULT 0,
updated_user BIGINT NOT NULL DEFAULT 0,
PRIMARY KEY (id)
);
USER Class:
#Entity(name="USER")
#Table(name="USERS")
public class User implements Audited {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="ID", nullable=false, updatable=false)
private long id;
#Column(name="DISPLAY_NAME", nullable=false)
#NotNull
#Size(min=5, max=30)
private String displayName;
#Column(name="ACTIVE", nullable=false)
#NotNull
private boolean active;
private Audit audit;
}
AUDIT Class:
public class Audit {
private Date createdTime;
private User createdByUser;
private Date updatedTime;
private User updatedByUser;
}
On each entity table, the audit fields will be named the same. What is the best approach in mapping these fields?

The best way would be to make the Audit class as #Embeddable entity and use it in your all entities as an #Embedded property, which is almost what your are doing currently.

Related

How to map a OneToOne with ZeroOrOneToOne back reference in JPA?

I have a legacy app which has an entity relationship that looks like this. I changed the names of the fields to less realistic values and reduced to only the relevant fields.
CREATE TABLE `billing_target` (
`billingTargetID` int(11) NOT NULL,
`targetType` char(5) NOT NULL,
`targetID` int(11) NOT NULL
PRIMARY KEY (`billingTargetID`)
);
CREATE TABLE `Client` (
`clientID` int(11) NOT NULL,
`name` varchar(200),
`color` varchar(200),
`shape` varchar(200)
PRIMARY KEY (`clientID`),
CONSTRAINT `fk_client_billingTarget`
FOREIGN KEY (`clientID`)
REFERENCES `billing_target` (`targetID`)
);
My most current attempt which causes an issue when saving as it gives a null entity key exception.
#Data
#Entity
public class BillingTarget implements Serializable {
#Id
#Column(name = "billingTargetID")
private Integer id;
#Column(name = "targetID", nullable = false)
private Integer targetID;
#Column(name = "targetType", nullable = false)
private String type;
}
#Data
#Entity
public class Client implements Serializable {
#Id
#Column(name = "clientID")
private Integer id;
#OneToOne
#JoinColumn(name = "clientID")
#MapsId("targetID")
private BillingTarget billingTarget;
private String name;
private String color;
private String shape;
}
Here's the PlantUML code if interested
#startuml
hide circle
entity BillingTarget {
* billingTargetID <<generated>>
--
* targetType
* targetID <<unique>>
}
entity Client {
* clientID <<fk>>
--
* name
color
shape
}
BillingTarget ||--o| Client : "targetID:clientID"
#enduml
I was already thinking of using a MappedSuperclass but right now it is only one type (though it could be more). Second the billing target may be zero and not null as there's a NOT NULL constraint already present.

How do I properly map entities where a primary key is composed of two foreign keys, one of which is composite itself?

I'm having a hard time trying to figure out how to properly do ORM on a certain database design.
My schema consists of three tables: a user table, a review table, and a vote table. Users can publish reviews for albums, and they can also assign a positive or negative rating to any review. Albums are provided from an external API, so their table is missing from the schema, but their IDs are referenced.
A user primary key simply consists of their username. A review primary key is composed of the reviewer's username, which is a foreign key, and the reviewed album ID. Finally, a vote primary key is composed of the voter's username, again a foreign key, and the voted review's primary key, consisting, as said earlier, of the reviewer's username and the reviewed album ID.
A user can publish a review for each individual album, and also can assign a vote for each individual review.
This is the ER model representing the schema:
To map the entities IDs, I'm using the #IdClass annotation, but I'm not sure I'm headed in the right direction. I also tried using the #EmbeddedId annotation, but the result is the same.
This is what my entities classes look like so far:
#Entity
public class User implements Serializable {
private static final long serialVersionUID = 1;
#Id #Column(name = "username")
private String username;
#Column(unique = true, nullable = false)
private String email;
#Column(name = "password", nullable = false)
private String password;
#Temporal(TemporalType.TIMESTAMP) #Column(name="signUpDate", nullable = false)
private Date signUpDate;
// empty constructor, getters, setters, equals and hashCode implementations
}
#Entity #IdClass(ReviewId.class)
public class Review implements Serializable {
private static final long serialVersionUID = 1;
#Id #ManyToOne #JoinColumn(name = "reviewerUsername", referencedColumnName = "username")
private User reviewer;
#Id #Column(name = "reviewedAlbumId")
private Long reviewedAlbumId;
#Column(name = "content", nullable = false, length = 2500)
private String content;
#Column(name = "rating", nullable = false)
private Integer rating;
#Temporal(TemporalType.TIMESTAMP) #Column(name = "publicationDate", nullable = false)
private Date publicationDate;
// empty constructor, getters, setters, equals and hashCode implementations
}
#Entity #IdClass(VoteId.class)
public class Vote implements Serializable {
private static final long serialVersionUID = 1;
#Id #ManyToOne #JoinColumn(name = "voterUsername", referencedColumnName = "username")
private User voter;
#Id #ManyToOne #JoinColumns({
#JoinColumn(name = "reviewerUsername", referencedColumnName = "reviewerUsername"),
#JoinColumn(name = "reviewedAlbumId", referencedColumnName = "reviewedAlbumId")
})
private Review review;
#Column(name = "vote") // #todo add attribute nullable = false
private Boolean vote;
// empty constructor, getters, setters, equals and hashCode implementations
}
These are my ID classes:
public class ReviewId implements Serializable {
private static final long serialVersionUID = 1L;
private User reviewer;
private Long reviewedAlbumId;
// empty constructor, getters, setters, equals and hashCode implementations
}
public static class VoteId implements Serializable {
private static final long serialVersionUID = 1L;
private User voter;
private Review review;
// empty constructor, getters, setters, equals and hashCode implementations
}
And here is the content of the MySQL script used to generate the schema:
DROP SCHEMA IF EXISTS albumReviewsDatabase;
CREATE SCHEMA albumReviewsDatabase;
USE albumReviewsDatabase;
CREATE TABLE user (
username VARCHAR(20) PRIMARY KEY,
email VARCHAR(254) NOT NULL UNIQUE,
password CHAR(60) NOT NULL,
signUpDate TIMESTAMP NOT NULL DEFAULT now()
) ENGINE = INNODB;
CREATE TABLE review (
reviewerUsername VARCHAR(20) NOT NULL,
reviewedAlbumId BIGINT(20) NOT NULL,
content TEXT NOT NULL,
rating SMALLINT UNSIGNED NOT NULL,
publicationDate TIMESTAMP NOT NULL DEFAULT now(),
CHECK (rating >= 0 AND rating <= 10),
PRIMARY KEY (reviewerUsername, reviewedAlbumId),
FOREIGN KEY (reviewerUsername) REFERENCES user(username)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE = INNODB;
CREATE TABLE vote (
voterUsername VARCHAR(20) NOT NULL,
reviewerUsername VARCHAR(20) NOT NULL,
reviewedAlbumId BIGINT(20) NOT NULL,
vote BOOLEAN NOT NULL,
PRIMARY KEY (voterUsername, reviewerUsername, reviewedAlbumId),
FOREIGN KEY (voterUsername) REFERENCES user(username)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (reviewerUsername, reviewedAlbumId) REFERENCES review(reviewerUsername, reviewedAlbumId)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE = INNODB;
I'm currently using OpenJPA as the persistence provider on a TomEE webprofile instance, and the used JPA version is 2.0.
Clearly I am misunderstating something about JPA's ORM, because when I deploy my application containing those entities I get the following exception:
<openjpa-2.4.2-r422266:1777108 fatal user error> org.apache.openjpa.util.MetaDataException: The id class specified by type "class application.model.Review" does not match the primary key fields of the class. Make sure your identity class has the same primary keys as your persistent type, including pk field types. Mismatched property: "reviewer"
The exception is thrown because of the Review class mapping, and not the Vote class; however, I am sure that by solving the issue on the Review class, the same will reappear for Vote.
I'd prefer to get away with using the #IdClass annotation instead of #EmbeddedId, but whichever of the two I will end up using is not an issue.
These relationships are "derived identities"; so your ID classes should look like this (note the types of the foreign key fields differ from the types of their corresponding entity fields):
public class ReviewId implements Serializable {
private static final long serialVersionUID = 1L;
private String reviewer; // matches name of #Id attribute and type of User PK
private Long reviewedAlbumId;
// ...
}
public static class VoteId implements Serializable {
private static final long serialVersionUID = 1L;
private String voter; // matches name of #Id attribute and type of User PK
private ReviewId review; // matches name of #Id attribute and type of Review PK
// ...
}
Derived identities are discussed (with examples) in the JPA 2.2 spec in section 2.4.1.
Also, as a side note, #IdClass is a bit Old School while #EmbeddedId is cleaner, eliminating the code duplicated across the entity and its key.

DynamicUpdate added extra untouched column in update statement

I am facing a weird behavior using #DynamicUpdate with one of my entity. So my entity is defined like this
#Entity
#DynamicUpdate
#Table(name = "courts")
#Getter
#Setter
#Builder
#AllArgsConstructor // require for #Builder to work correctly
#NoArgsConstructor // required for hibernate mapping
public class CourtDO {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// name is SQL keyword
#Column(name = "court_name")
private String name;
#Column
private String addressStreet;
#Column
private String addressWard;
#Column
private String addressDistrict;
#Column
private String addressCity;
#Column
private String addressCountry;
#Column
private String phoneNumber;
#Column(nullable = false)
#Convert(converter = DOTimestampConverter.class)
#CreationTimestamp
private ZonedDateTime createdAt;
#Column(nullable = false)
#Convert(converter = DOTimestampConverter.class)
#UpdateTimestamp
private ZonedDateTime updatedAt;
}
DOTimestampConverter is just a simple AttributeConverter to convert ZonedDateTime to ms for me to store in DB as number.
As you can see I marked the entity with #DynamicUpdate.
I have a small jersey REST-API which allow me to update all of the normal field beside the id and the generated date. I always check if the input is null before actually touching the setter for the field.
I see a very weird behavior with the column address_city, it would be included in every update like this even if I only touch other fields, in this case, just the name which translate to court_name because name is a reserved keyword in SQL. UPDATE Actually the problem is not with just the address_city column. Even if I ignore that column, other column would get included in the update statement.
Hibernate:
update
courts
set
address_city=?,
court_name=?,
updated_at=?
where
id=?
Where as if I only edit the address_city only then I would see it correctly
Hibernate:
update
courts
set
address_city=?,
updated_at=?
where
id=?
Did anyone face this problem before?
The stack I am using are
spring-boot 2.1.3.RELEASE
spring-boot-starter-data-jpa
postgresql 11.4
And the data schema
CREATE TABLE COURTS (
id SERIAL PRIMARY KEY,
court_name VARCHAR(20),
address_street VARCHAR(128),
address_ward VARCHAR(20),
address_district VARCHAR(20),
address_city VARCHAR(20),
address_country VARCHAR(10),
phone_number VARCHAR(20),
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL
);
This is how update through REST-API
#Override
public Court editCourt(String courtId, CreateCourtRequest createCourtRequest) {
Optional<CourtDO> court = courtRepository.findById(NumberUtils.toLong(courtId));
return court
.map(courtDO -> editCourtInfo(courtDO, createCourtRequest))
.map(courtRepository::save)
.map(courtDOToResponseConverter::convert)
.orElse(null);
}
private CourtDO editCourtInfo(CourtDO courtDO, CreateCourtRequest createCourtRequest) {
if (StringUtils.isNotBlank(createCourtRequest.getName())) {
courtDO.setName(createCourtRequest.getName());
}
if (StringUtils.isNotBlank(createCourtRequest.getAddressStreet())) {
courtDO.setAddressStreet(createCourtRequest.getAddressStreet());
}
if (StringUtils.isNotBlank(createCourtRequest.getAddressWard())) {
courtDO.setAddressWard(createCourtRequest.getAddressWard());
}
if (StringUtils.isNotBlank(createCourtRequest.getAddressDistrict())) {
courtDO.setAddressDistrict(createCourtRequest.getAddressDistrict());
}
if (StringUtils.isNotBlank(createCourtRequest.getAddressCity())) {
courtDO.setAddressCity(createCourtRequest.getAddressCity());
}
if (StringUtils.isNotBlank(createCourtRequest.getPhoneNumber())) {
courtDO.setPhoneNumber(createCourtRequest.getPhoneNumber());
}
return courtDO;
}

Spring boot REST CRUD - how to POST an entitiy with a one-to-one relationship?

I have a really simple domain model: An 'Alert' has one 'Type' and one 'Status'.
This is my schema:
create table `price_alert_status` (
`id` bigint(20) not null,
`status_name` varchar(64) not null,
primary key (`id`),
unique key (`status_name`)
) engine=InnoDB default charset=utf8;
insert into `price_alert_status` values (0, 'INACTIVE');
insert into `price_alert_status` values (1, 'ACTIVE');
create table `price_alert_type` (
`id` bigint(20) not null,
`type_name` varchar(64) not null,
primary key (`id`),
unique key (`type_name`)
) engine=InnoDB default charset=utf8;
insert into `price_alert_type` values (0, 'TYPE_0');
insert into `price_alert_type` values (1, 'TYPE_1');
create table `price_alert` (
`id` bigint(20) not null auto_increment,
`user_id` bigint(20) not null,
`price` double not null,
`price_alert_status_id` bigint(20) not null,
`price_alert_type_id` bigint(20) not null,
`creation_date` datetime not null,
`cancelation_date` datetime null,
`send_periodic_email` tinyint(1) not null,
`price_reached_notifications` tinyint(4) default '0',
`approximate_price_notifications` tinyint(4) null,
`notify` tinyint(1) not null default '1',
primary key (`id`),
constraint `FK_ALERT_TO_ALERT_STATUS` foreign key (`price_alert_status_id`) references `price_alert_status` (`id`),
constraint `FK_ALERT_TO_ALERT_TYPE` foreign key (`price_alert_type_id`) references `price_alert_type` (`id`)
) engine=InnoDB default charset=utf8;
Now, I'm going to show the respective entity classes:
Alert.java:
// imports omitted
#Entity
#Table(name = "price_alert")
#EntityListeners(AuditingEntityListener.class)
#JsonIgnoreProperties(value = {"creationDate"},
allowGetters = true)
public class Alert implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long userId;
private double price;
#OneToOne
#JoinColumn(name = "price_alert_status_id", nullable = false)
private Status status;
#OneToOne
#JoinColumn(name = "price_alert_type_id", nullable = false)
private Type type;
#Column(nullable = false, updatable = false)
#Temporal(TemporalType.TIMESTAMP)
#CreatedDate
private Date creationDate;
#Column(nullable = true)
#Temporal(TemporalType.TIMESTAMP)
private Date cancelationDate;
private boolean sendPeriodicEmail;
#Column(nullable = true)
private byte priceReachedNotifications;
#Column(nullable = true)
private byte approximatePriceNotifications;
private boolean notify;
// getters and setters omitted
}
Status.java:
//imports omitted
#Entity
#Table(name = "price_alert_status")
public class Status implements Serializable {
private static final long serialVersionUID = 1L;
#Id
private Long id;
#Column(name = "status_name")
#NotBlank
private String name;
//getters and setters omitted
}
Type.java:
//imports omitted
#Entity
#Table(name = "price_alert_type")
public class Type implements Serializable {
private static final long serialVersionUID = 1L;
#Id
private Long id;
#Column(name = "type_name")
#NotBlank
private String name;
//getters and setters omitted
}
Repositories:
AlertRepository.java:
//imports omitted
#Repository
public interface AlertRepository extends JpaRepository<Alert, Long> {
}
StatusRepository.java:
//imports omitted
#Repository
public interface StatusRepository extends JpaRepository<Status, Long> {
}
TypeRepository.java:
//imports omitted
#Repository
public interface TypeRepository extends JpaRepository<Type, Long> {
}
Now, the main controller:
AlertController.java:
#RestController
#RequestMapping("/api")
public class AlertController {
#Autowired
AlertRepository alertRepository;
#Autowired
StatusRepository statusRepository;
#Autowired
TypeRepository typeRepository;
#GetMapping("/alerts")
public List<Alert> getAllAlerts() {
return alertRepository.findAll();
}
#PostMapping("/alert")
public Alert createAlert(#Valid #RequestBody Alert alert) {
return alertRepository.save(alert);
}
#GetMapping("/alert/{id}")
public Alert getAlertById(#PathVariable(value = "id") Long alertId) {
return alertRepository.findById(alertId)
.orElseThrow(() -> new ResourceNotFoundException("Alert", "id", alertId));
}
#PutMapping("/alert/{id}")
public Alert updateAlert(#PathVariable(value = "id") Long alertId,
#Valid #RequestBody Alert alertDetails) {
Alert alert = alertRepository.findById(alertId)
.orElseThrow(() -> new ResourceNotFoundException("Alert", "id", alertId));
alert.setApproximatePriceNotifications(alertDetails.getApproximatePriceNotifications());
alert.setCancelationDate(alertDetails.getCancelationDate());
alert.setNotify(alertDetails.isNotify());
alert.setPrice(alertDetails.getPrice());
alert.setPriceReachedNotifications(alertDetails.getPriceReachedNotifications());
alert.setSendPeriodicEmail(alertDetails.isSendPeriodicEmail());
alert.setUserId(alertDetails.getUserId());
// TODO: how to update Status and Type?
Alert updatedAlert = alertRepository.save(alert);
return updatedAlert;
}
#DeleteMapping("/alert/{id}")
public ResponseEntity<?> deleteAlert(#PathVariable(value = "id") Long alertId) {
Alert alert = alertRepository.findById(alertId)
.orElseThrow(() -> new ResourceNotFoundException("Alert", "id", alertId));
alertRepository.delete(alert);
return ResponseEntity.ok().build();
}
}
So, I have two questions:
How can I create an alert, via POST, and associate existing status and type?
For example, this would be my cURL. I'm trying to indicate that I want to associate to this new alert the 'Status' and 'Type' existing objects, passing their respective IDs:
curl -H "Content-Type: application/json" -v -X POST localhost:8080/api/alert -d '{"userId": "1", "price":"20.0", "status": {"id": 0}, "type": {"id": 0}, "sendPeriodicEmail":false,"notify":true}'
Like the first question, how can I update an Alert, associating new existing 'Status' and 'Type' objects?
Thanks!
I think there is no out-of-the-box way to achieve this with a single POST request. The approach I see used most of the time is making an initial request to create the Alert, and subsequent requests to associate Status and Type.
You could take a look at how Spring Data Rest approaches the problem here:
https://reflectoring.io/relations-with-spring-data-rest/
https://docs.spring.io/spring-data/rest/docs/current/reference/html/#repository-resources.association-resource
I'm not the biggest fan of Spring Data Rest though, since it forces some things (like hateoas) down your throat
,but you can easily implement the same approach manually.
You could argue that it's overkill to have separate calls to set the status and type of an alert, being both actually part of the alert, and I may agree actually. So if you don't mind slightly deviating from the rigidity of what people mostly call REST APIs (but are more like CRUD interfaces exposing your data model), it could make sense to take an AlertDto (with status and type ids) in your alert creation endpoint, retrieve status and type with these ids and create the Alert object you will eventually store.
Having said all of the above, I would avoid having tables for Status and Type if all they have is a name. I would have these names in the Alert itself and no relationships at all. Yes it may occupy more space on the database, but disk space is hardly a problem nowadays, and I'm guessing status and type are usually short strings.
I admit I am specially biased against this id-name lookup table pattern because we have dozens of these in one of our projects at work and they do nothing but generate a lot of useless code and complicate the DB schema.

HIbernate one-to-one annotation isn't generating foreign key GerericGenerator in dependent table

I am trying to create OneToOne relation between a Person and Auth table. The problem is when the DB table "Auth" is generated, I'm not seeing the foreign key in the AUTH table that should reference Person. The object is to have the Auth table use the same Primary Key of the Person Table.
#MappedSuperclass
public abstract class DomainBase {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
#Version
#Column(name="OPLOCK")
private Integer version;
}
#Entity
#Table(name = "person")
public class Person extends DomainBase {
#OneToOne(cascade=CascadeType.ALL)
#JoinColumn(name="auth_id")
private Auth auth;
}
#Entity
public class Auth {
#Id
#GeneratedValue(generator="foreign")
#GenericGenerator(name="foreign", strategy = "foreign", parameters={
#Parameter(name="property", value="person")
})
#Column(name="person_id")
private int personId;
---------------------------------
#OneToOne(cascade = CascadeType.ALL)
#PrimaryKeyJoinColumn
private Person person;
}
My Database scripts after hibernate DB generation.
CREATE TABLE auth
(
person_id integer NOT NULL,
activate boolean,
activationid character varying(255),
last_login_attempt_date timestamp without time zone,
last_login_attempt_timezone character varying(255),
last_login_date timestamp without time zone,
last_login_timezone character varying(255),
nonlocked boolean,
num_login_attempts integer,
CONSTRAINT auth_pkey PRIMARY KEY (person_id),
CONSTRAINT uk_d68auh3xsosyrjw3vmwseawvt UNIQUE (activationid)
)
WITH (
OIDS=FALSE
);
ALTER TABLE auth
OWNER TO postgres;
It seems that the problem is you declare twice the #OneToOne annotation between "person" table and "auth" table, without specify the relation between them. Take a look at the hibernate documentation, at the point 2.2.5.1, there is some examples about using one-to-one association.
For me, the best way is to set up the association in one table, the one that declare the foreing key column, and to use the mappedBy parameter in the other object. In your code, this will be :
#Entity
#Table(name = "person")
public class Person extends DomainBase {
#OneToOne(cascade=CascadeType.ALL)
#JoinColumn(name="auth_id")
private Auth auth;
}
#Entity
public class Auth {
#Id
#GeneratedValue(generator="foreign")
#GenericGenerator(name="foreign", strategy = "foreign", parameters={
#Parameter(name="property", value="person")
})
#Column(name="person_id")
private int personId;
#OneToOne(mappedBy = "auth")
private Person person;
....
}
This is the second example in the hibernate documentation, introduce just after the sentence "In the following example, the associated entities are linked through an explicit foreign key column". I tested this code, and the "auth_id" column appeared.

Categories

Resources