实体创建无用的 id 字段

问题描述 投票:0回答:2

我有一个

CrudRepository
有两个实体。

问题:特征实体总是在数据库中创建额外的

id
字段,但没有选择正确的
characteristic_id
字段来生成
JSON

machine
实体:

machine_id || name || description

characteristic
实体:

characteristic_id || machine_id || name || description || type || value

问题: 为什么特征实体总是创建名为

id
的id字段而不使用定义的字段
characteristic_id

JSON:

实体如何操作数据库布局:

machine
实体:

@Entity
@Table(name = "maschine")
public class Machine {
    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;
    }

}

characteristic
实体:

@Entity
@Table(name = "characteristic")
public class Characteristic {
    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();
    }
    
}

这是我为数据库操作编写的所有代码..

json spring-boot hibernate rest entity-framework
2个回答
1
投票

更改为此代码

@Id
@GeneratedValue
@Column(name="id", nullable = false)
public int getCharacteristic_id() {
    return characteristic_id;
}

1
投票

默认情况下,由于您没有在配置中指定列名,hibernate 将使用不带“get”部分的 getter 方法的名称作为表中的列名。以下是您的特征实体中的配置

@Id
@GeneratedValue
public int getId() {
    return characteristic_id;
}

由于您没有为成员characteristic_id指定任何明确的列名,hibernate默认选择Id(从getId()方法中删除get)作为成员的列名。因此它总是为您创建一个名为 Id 的列 features_id member 。

为了强制hibernate使用characteristic_id作为列名,您可以使用@Column注释并提供列名作为characteristics_id 除了您用来标记 getter 的 @Id 和 @GenerateValue 之外。

@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;
}
© www.soinside.com 2019 - 2024. All rights reserved.