[英]using embedded id type twice in an entity
In my spring-boot project, I have a custom type for ID field of my entity. 在我的spring-boot项目中,我对实体的ID字段有一个自定义类型。 I can use the type with the help of
@Embeddable
and @EmbeddedId
. 我可以在
@Embeddable
和@EmbeddedId
的帮助下使用该类型。
But when I want to add another field with the same type into a single entity, I get a column mapping exception as follows: 但是,当我想将另一个具有相同类型的字段添加到单个实体中时,会出现如下列映射异常:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity:
com.example.demo.CarEntity column: id (should be mapped with insert="false" update="false")
CarId class: CarId类别:
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Embeddable
public class CarId implements Serializable {
private String id;
@Override
public String toString() {
return id;
}
}
CarEntity class: CarEntity类:
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Entity
public class CarEntity {
@EmbeddedId
private CarId id;
private String name;
private CarId anotherId;
}
Repository class: 存储库类:
@Repository
public interface CarRepository extends JpaRepository<CarEntity, CarId> {
}
application class: 应用类别:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
CommandLineRunner runner(CarRepository repository) {
return args -> {
CarId carId = new CarId(UUID.randomUUID().toString());
String carName = "a car";
CarEntity carEntity = new CarEntity(carId, carName, carId);
repository.save(carEntity);
repository.findAll().forEach(carEntity1 -> {System.out.println(carEntity1.getId());});
};
}
}
How can I add multiple fields with the same type into the entry class? 如何将多个具有相同类型的字段添加到条目类中?
The solution I found is to override the field name on anotherId
as follows: 我发现的解决方案是按如下方式覆盖
anotherId
上的字段名称:
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Entity
public class CarEntity {
@EmbeddedId
private CarId id;
private String name;
@Embedded
@AttributeOverride(name="id", column = @Column(name = "anotherId"))
private CarId anotherId;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.