JPA Hibernate映射了2个以上的实体

时间:2017-01-12 10:01:41

标签: java hibernate jpa

我想以下列方式映射我的实体:

我有一位用户,他购买了一辆汽车(一个特定的品牌,型号和型号)。

为此,我创建了以下类。

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实现这样的功能。

我可以定义与@ManyToManyUser的{​​{1}}关系。在这种情况下,如果我Caruser.getCars().getModels(),我最终会获得数据库中的所有内容。

如何以这样的方式映射user.getCars().getModels().get(0).getVariants()User以获得以下输出?

Car

另外,如果您认为我还没有设计好我的实体课程,请随时告诉我如何改进。

由于

2 个答案:

答案 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
    ......
}
  1. 避免Collection s:尽可能使用SetLinkedHashSet),或List时需要@OrderColumn
  2. 提供简单的名称:避免MODEL_NAME,只需使用NAME
  3. 使用@MappedSuperclass,特别是如果您拥有所有实体的代理ID
  4. 你现在可以:

    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'";