rephrasing:
i have finite types of vehicles. the Vehicle class has a Make class, no problem there as Vehicle as a FK to the make. For distinguishing wich available Makes apply to each vehicle, they also have a type. so the type is shared and of the same domain.
- i mapped the types in an enum (VehicleType)
- used @MappedSuperclass in BaseEntity
- @Inheritance(strategy = InheritanceType.JOINED) in Vehicle
- the Vehicle class is abstract and the contructor protected so the developer (me) has to call super in the descendant classes (Car), with an VehicleType value, defining the type
- both Car and Make have :
@Enumerated(EnumType.ORDINAL)
private VehicleType vehicleType;
BaseEntity
-> Vehicle
-> Car
-> MotorCycle
i think i used both the enum and the inheritance. my ingenious super mind (not) has come up with this:
Code:
pt.standout.db.core.domain.base.BaseEntity.java
------------------------------------------------------
package pt.standout.db.core.domain.base;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
/**
* @author josemoreira
* The mother of all entities
*
*/
@MappedSuperclass
public class BaseEntity implements Serializable {
static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
/**
* @return
*/
public Long getId() {
return id;
}
/**
* @param id
*/
public void setId(Long id) {
this.id = id;
}
}
pt.standout.db.catalog.domain.Vehicle.java
------------------------------------------------------
package pt.standout.db.catalog.domain;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import pt.standout.db.catalog.usertype.VehicleType;
import pt.standout.db.core.domain.base.BaseEntity;
/**
* @author josemoreira
*
*/
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Vehicle extends BaseEntity {
/**
*
*/
private static final long serialVersionUID = 1L;
@Enumerated(EnumType.ORDINAL)
private VehicleType vehicleType;
protected Vehicle(VehicleType vehicleType) {
this.vehicleType = vehicleType;
}
public VehicleType getVehicleType() {
return vehicleType;
}
}
pt.standout.db.catalog.domain.Car.java
------------------------------------------------------
package pt.standout.db.catalog.domain;
import javax.persistence.Entity;
import javax.persistence.Table;
import pt.standout.db.catalog.usertype.VehicleType;
/**
* @author ethernal
*
*/
@Entity
@Table(name="catalog_car")
public class Car extends Vehicle {
/**
*
*/
private static final long serialVersionUID = 1L;
public Car() {
super(VehicleType.CAR);
}
}
pt.standout.db.catalog.usertype.VehicleType.java
------------------------------------------------------
/**
* The vehicle type (car, boat, motorcycle)
*/
package pt.standout.db.catalog.usertype;
import java.util.ArrayList;
import java.util.List;
/**
* @author josemoreira
*
*/
public enum VehicleType {
CAR(1), BOAT(2), MOTORCYCLE(3);
private int type;
private static final List<VehicleType> types = new ArrayList<VehicleType>();
static {
for (VehicleType type : VehicleType.values())
types.add(type);
}
public int value() {
return type;
}
private VehicleType(int type) {
this.type = type;
}
}
what do you think?
The Vehicle should also have a Model property that i havent added yet.
also is there a way to enforce (or should i) that a vehicle cant have a Model for wich the types anrent the same, through annotations?
thank you \O/