jublikon jublikon - 1 month ago 6
JSON Question

Entity creates useless id field

I have a

CrudRepository
with two entities.

Problem: The characteristic entity always creates an additional id field in the database but does not select the correct
characteristic_id
field to generate
JSON
.

machine entity

machine_id || name || description


characteristic entity

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


Question: Why does the characteristic entity always create an id field named
id
and does not use the definded filed
characteristic_id
?

JSON
enter image description here

How the entity manipulates the database layout:
enter image description here

Machine entity

@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

@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 + "]";
}


}

My CrudRepository to interact with the database:

public interface MachineRepository extends CrudRepository<Machine, Integer>{

}


My Controller:

@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();
}

}


This is all code I have written for the database operations..

Answer

Change to this code

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