我想以下列方式映射我的实体:
我有一位用户,他购买了一辆汽车(一个特定的品牌,型号和型号)。
为此,我创建了以下类。
Car.java
@Entity
@Table(name="CARS")
public class Car {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="CAR_ID")
private int id;
@Column(name="CAR_MAKER")
private String maker;
@OneToMany(mappedBy="car")
private Collection<CarModel> models =new ArrayList<CarModel>();
......
Getters and Setters go here
......
}
CarModel.java
@Entity
@Table(name="CAR_MODELS")
public class CarModel {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="MODEL_ID")
private int modelId;
@Column(name="MODEL_NAME")
private String modelName;
@ManyToOne
@JoinColumn(name="CAR_ID")
private Car car;
@OneToMany(mappedBy="model")
private Collection<CarVariant> variants = new ArrayList<CarVariant>();
......
Getters and Setters go here
......
}
CarVariant.java
@Entity
@Table(name="CAR_VARIANT")
public class CarVariant {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column (name="VARIANT_ID")
private int variantId;
@Column(name="VARIANT_NAME")
private String variantName;
@Column(name="FUEL_TYPE")
private String fuelType;
@ManyToOne
@JoinColumn(name="MODEL_ID")
private CarModel model;
..........
Getters and setters go here
..........
}
最后, User.java
@Entity
@Table(name="MASUSER")
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="USER_REC_ID")
private int recordId;
@Column(name="USER_ID")
private String id;
@Column(name="USER_NAME")
private String name;
@Column(name="USER_EMAIL_ID")
private String emailId;
@Column(name="USER_PHONE")
private String phone;
private Collection<Car> cars = new ArrayList<Car>();
}
现在,这是我的问题。
在现实生活中,用户可能拥有许多汽车,每辆汽车都有特定的品牌,型号和型号。我想用JPA / Hibernate实现这样的功能。
我可以定义与@ManyToMany
和User
的{{1}}关系。在这种情况下,如果我Car
或user.getCars().getModels()
,我最终会获得数据库中的所有内容。
如何以这样的方式映射user.getCars().getModels().get(0).getVariants()
和User
以获得以下输出?
Car
另外,如果您认为我还没有设计好我的实体课程,请随时告诉我如何改进。
由于
答案 0 :(得分:1)
如果您像这样获取主要对象
Criteria criteria=session.createCriteria(User.class);
List<Object> list=criteria.list();
User user=(User)list.get(0);
Car car=user.getCar();
CarModel carModel=car.getCarModel();
CarVarient carVarient=carModel.getCarVarient();
通过这种方式,您可以获取您尝试获取的每个对象,但请记住,您正在使用FethType.Lazy
和cascadeType.All in everyPojo
答案 1 :(得分:0)
目前尚不清楚为什么需要这样一个包含嵌套关系中所有键的表。
我的两分钱:
@MappedSuperclass
public abstract class AbstractEntity
{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column
private Long id;
......
Getters and Setters go here
......
}
@Entity
@Table(name = "MANUFACTURER")
public class Manufacturer extends AbstractEntity
{
@Column
private String name;
@OneToMany(mappedBy = "manufacturer")
private Set<CarModel> models = new LinkedHashSet<>();
......
Getters and Setters go here
......
}
@Entity
@Table(name = "CAR_MODEL")
public class CarModel extends AbstractEntity
{
@NotNull
@ManyToOne(optional = false)
@JoinColumn(name = "MANUFACTURER_ID")
private Manufacturer manufacturer;
@Column
private String name;
@OneToMany(mappedBy = "model")
private Set<CarVariant> variants = new LinkedHashSet<>();
......
Getters and Setters go here
......
}
@Entity
@Table(name = "CAR_VARIANT")
public class CarVariant extends AbstractEntity
{
@NotNull
@ManyToOne(optional = false)
@JoinColumn(name = "CAR_MODEL_ID")
private CarModel model;
@Column
private String name;
@Column(name="FUEL_TYPE")
private String fuelType;
@OneToMany(mappedBy="variant")
private Set<Car> cars = new LinkedHashSet<>();
......
Getters and Setters go here
......
}
@Entity
@Table(name = "CAR")
public class Car extends AbstractEntity
{
@NotNull
@ManyToOne(optional = false)
@JoinColumn(name = "CAR_VARIANT_ID")
private CarVariant variant;
@Column(name = "REGISTRATION_NUMBER")
private String registrationNumber;
@ManyToMany(mappedBy = "cars")
private Set<User> users = new LinkedHashSet<>();
......
Getters and Setters go here
......
}
@Entity
@Table(name = "USER")
public class User extends AbstractEntity
{
@Column
private String name;
@Column
private String email;
@Column
private String phone;
@ManyToMany
@JoinTable(name = "USER_CAR", joinColumns = @JoinColumn(name = "USER_ID"), inverseJoinColumns = @JoinColumn(name = "CAR_ID"))
private Set<Car> cars = new LinkedHashSet<>();
......
Getters and Setters go here
......
}
Collection
s:尽可能使用Set
(LinkedHashSet
),或List
时需要@OrderColumn
MODEL_NAME
,只需使用NAME
@MappedSuperclass
,特别是如果您拥有所有实体的代理ID Car car = ...;
String manufacturerName = car.getVariant().getModel().getManufacturer().getName();
String jpql = "select c from Car c where c.variant.model.name = 'Golf' and c.variant.fuelType = 'Diesel'";