我有CrudRepository
个实体。
问题:特征实体始终在数据库中创建其他ID字段,但未选择正确的characteristic_id
字段来生成JSON
。
机器实体
machine_id || name || description
特色实体
characteristic_id || machine_id || name || description || type || value
问题:为什么特征实体始终会创建名为id
的ID字段,而不使用定义的字段characteristic_id
?
机器实体
@Entity
@Table(name =" maschine") 公共类机器{ private int machine_id;
private String name;
private String description;
private Set<Characteristic> characteristics;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "machine", cascade = CascadeType.ALL)
public Set<Characteristic> getCharacteristics() {
return characteristics;
}
public void setCharacteristics(Set<Characteristic> characteristics){
this.characteristics = characteristics;
}
public Machine(){}
public Machine(String name, String description){
this.name = name;
this.description = description;
}
@Override
public String toString() {
return "Machine [id=" + machine_id + ", name=" + name + ", description=" + description + "]";
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
public int getId() {
return machine_id;
}
public void setId(int machine_id) {
this.machine_id = machine_id;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
特色实体
@Entity
@Table(名字=&#34;特色&#34;) 公共阶层特征{ private int characteristic_id;
private String name;
private String description;
private int type;
private int value;
private Machine machine;
@ManyToOne
@JoinColumn(name="machine_id")
public Machine getMachine(){
return machine;
}
public void setMachine(Machine machine){
this.machine = machine;
}
public Characteristic() {}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id")
public int getCharacteristic_Id() {
return characteristic_id;
}
public void setCharacteristic_Id(int characteristic_id) {
this.characteristic_id = characteristic_id;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name="type")
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Column(name="value")
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
@Override
public String toString() {
return "Characteristic [id=" + characteristic_id + ", name=" + name + ", description=" + description + ", type=" + type
+ ", value=" + value + "]";
}
}
我的CrudRepository与数据库交互:
public interface MachineRepository extends CrudRepository<Machine, Integer>{
}
我的控制器:
@RestController
public class HomeController {
private final MachineRepository machineRepository;
@Autowired
HomeController(MachineRepository machineRepository) {
this.machineRepository = machineRepository;
}
@RequestMapping(value = "/machine", method = RequestMethod.GET)
Collection<Machine> readMachines(){
return (Collection<Machine>) machineRepository.findAll();
}
}
这是我为数据库操作编写的所有代码..
答案 0 :(得分:1)
默认情况下,由于您未在配置中指定列名,因此hibernate将使用getter方法的名称,而不使用“get”部分作为表中的列名。以下是特征实体
中的配置@Id
@GeneratedValue
public int getId() {
return characteristic_id;
}
由于您没有为memeber characteristic_id指定任何显式列名,因此默认情况下,hibernate选择Id(从getId()方法中删除get)作为成员的列名。因此,它总是为您的characteristics_id成员创建一个名为Id的列。
为了强制hibernate使用characteristic_id作为列名,可以使用@Column注释并将列名提供为characteristics_id 除了用于标记吸气剂的@Id和@GeneratedValue之外。
@Id
@GeneratedValue
@Column(name="characteristic_id")
public int getId() {
return characteristic_id;
}
或者您可以更改getter和setter的名称,如下所示
@Id
@GeneratedValue
public int getCharacterisitc_id() {
return characteristic_id;
}
public void setCharacterisitc_id(int characteristic_id) {
this.characteristic_id = characteristic_id;
}
答案 1 :(得分:1)
更改为此代码
@Id
@GeneratedValue
@Column(name="id", nullable = false)
public int getCharacteristic_id() {
return characteristic_id;
}