相关疑难解决方法(0)

JPA - 坚持一对多的关系

也许这是一个愚蠢的问题,但它让我烦恼.

我有一个双向的员工与车辆的一对多关系.当我第一次在数据库中保留一个Employee(即它没有分配的ID)时,我也希望它的相关Vehicle保持不变.

这对我来说很好,除了我保存的Vehicle实体没有自动映射关联的Employee,并且在数据库中,Vehicle表中的employee_id外键列为null.

我的问题是,在员工本身被持久化的同时,是否有可能让车辆的员工坚持下去?我意识到需要首先保存员工,然后保存车辆.JPA可以自动为我做这个吗?或者我必须做以下事情:

Vehicle vehicle1 = new Vehicle();
Set<Vehicle> vehicles = new HashSet<Vehicle>();
vehicles.add(vehicle1);

Employee newEmployee = new Employee("matt");
newEmployee.setVehicles(vehicles);
Employee savedEmployee = employeeDao.persistOrMerge(newEmployee);

vehicle1.setAssociatedEmployee(savedEmployee);
vehicleDao.persistOrMerge(vehicle1);
Run Code Online (Sandbox Code Playgroud)

谢谢!

编辑:根据要求,这是我的映射(没有所有其他方法等)

@Entity
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="employee_id")
    private Long id;

    @OneToMany(mappedBy="associatedEmployee", cascade=CascadeType.ALL)
    private Set<Vehicle> vehicles;

    ...

}

@Entity 
public class Vehicle {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="vehicle_id")
    private Long id;

    @ManyToOne
    @JoinColumn(name="employee_id")
    private Employee associatedEmployee;

    ...
}
Run Code Online (Sandbox Code Playgroud)

我刚刚意识到我应该在Employee类上定义以下方法:

public void addVehicle(Vehicle vehicle) {
    vehicle.setAssociatedEmployee(this);
    vehicles.add(vehicle);
}
Run Code Online (Sandbox Code Playgroud)

现在上面的代码如下所示:

Vehicle …
Run Code Online (Sandbox Code Playgroud)

java orm jpa one-to-many

52
推荐指数
2
解决办法
9万
查看次数

Hibernate:insertable = false,updatable = false属于涉及外键的复合主键星座?

在Hibernate或其他ORM中实现复合主键时,最多有三个位置将insertable = false,updatable = false放在使用标识关系的复合主键星座中(FK是PK的一部分):

  1. 进入复合PK类'@Column注释(仅限@Embeddable类)或
  2. 进入实体类'association @ JoinColumn/s注释或
  3. 进入实体类' 冗余 PK属性的@Column注释(仅限@IdClass类)

第三种是使用@IdClass和JPA 1.0 AFAIK的唯一方法.请参阅http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_Relationships.我只会考虑案例1.和2.

问:将"insertable = false,updatable = false"置于一般的首选位置是哪种方式?

我遇到过关于这个问题的Hibernate问题.例如,Hibernate 3.5.x会抱怨Zips表

CREATE TABLE Zips
(
  country_code CHAR(2),
  code VARCHAR(10),
  PRIMARY KEY (country_code, code),
  FOREIGN KEY (country_code) REFERENCES Countries (iso_code)
)
Run Code Online (Sandbox Code Playgroud)

有:

org.hibernate.MappingException: Repeated column in mapping for entity: com.kawoolutions.bbstats.model.Zip column: country_code (should be mapped with insert="false" update="false")
org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)
org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:698)
...
Run Code Online (Sandbox Code Playgroud)

如您所见,country_code列是PK和FK.这是它的类:

实体类:

@Entity
@Table(name = "Zips")
public class Zip implements Serializable
{
    @EmbeddedId
    private ZipId id;

    @ManyToOne …
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa relationships composite-primary-key

32
推荐指数
1
解决办法
7万
查看次数