Skip to content

Commit

Permalink
Merge pull request #3 from The-Champions-JV/db-create-car-entity
Browse files Browse the repository at this point in the history
[model] create car entity
  • Loading branch information
vsych321 authored Nov 20, 2023
2 parents e0ab3cf + a7699b8 commit f15876b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/main/java/com/champions/carsharingservice/model/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.champions.carsharingservice.model;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

@Entity
@Getter
@Setter
@EqualsAndHashCode(exclude = {"rentals"})
@ToString(exclude = {"rentals"})
@SQLDelete(sql = "UPDATE cars SET is_deleted = true WHERE id = ?")
@Where(clause = "is_deleted=false")
@Table(name = "cars")
public class Car {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private String model;

@Column(nullable = false)
private String brand;

@Enumerated(EnumType.STRING)
@Column(nullable = false, unique = true)
private CarType type;

@Column(nullable = false)
private Integer inventory;

@Column(nullable = false, name = "daily_fee")
private BigDecimal dailyFee;

@OneToMany(mappedBy = "car", orphanRemoval = true, cascade = CascadeType.REMOVE)
private Set<Rental> rentals = new HashSet<>();

@Column(nullable = false, name = "is_deleted")
private boolean isDeleted = false;

public enum CarType {
SEDAN,
SUV,
HATCHBACK,
UNIVERSAL
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/champions/carsharingservice/model/Rental.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.champions.carsharingservice.model;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Entity
public class Rental {
@Id
private Long id;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "car_id", nullable = false)
private Car car;
}

0 comments on commit f15876b

Please sign in to comment.