Попытка сделать однозначную двустороннюю реализацию приводит к попытке присвоить идентификатор из нулевого однозначного свойства - PullRequest
0 голосов
/ 13 февраля 2020

У меня есть один абстрактный класс, и я пытаюсь создать несколько конкретных классов. В этом конкретном классе у меня есть @OneToOne Releationship. Так что я пытаюсь сделать это optinal, но это приводит к ошибке org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property, и я действительно не понимаю, почему. так что, возможно, кто-то может объяснить мне. Вот мой код Абстрактный класс:

public abstract class ReefTankAbstract {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "reefTankIDgenerator")
@SequenceGenerator(name = "reefTankIDgenerator", sequenceName = "reefTankIDSequenz", allocationSize = 10, initialValue = 10)
@Column(name = "reefTankid")
private long id;public ReefTankAbstract(long id, String rtManufacturer, String rtManufacturerTyp, String rtDescription,
        LocalDate rtCreatedOn, double rtGrossVolume, double rtNetVolume, double rtLength, double rtWidth,
        double rtHeight, double rtFillingQuantity, List<Messung> messureWaterValues) {
    super();

конкретный класс:

public class ReefTankOnlySandRocks extends ReefTankAbstract {

@OneToOne(mappedBy = "reefTankOnlySandRocks", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = true)
private Sand sand;

@OneToOne(mappedBy = "reefTankOnlySandRocks", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = true)
private Rock rock;


public ReefTankOnlySandRocks(long id, String rtManufacturer, String rtManufacturerTyp, String rtDescription,
        LocalDate rtCreatedOn, double rtGrossVolume, double rtNetVolume, double rtLength, double rtWidth,
        double rtHeight, double rtFillingQuantity, List<Messung> messureWaterValues, Sand sand, Rock rock) {
    super(id, rtManufacturer, rtManufacturerTyp, rtDescription, rtCreatedOn, rtGrossVolume, rtNetVolume, rtLength,
            rtWidth, rtHeight, rtFillingQuantity, messureWaterValues);
    this.sand = sand;
    this.rock = rock;
}

Класс отношений

public class Sand {

@Id
private long id;

@Column(name = "sandManufacturer")
private String sManufacturer;

@Column(name = "sandDescription")
private String sDescription;

@Column(name = "sandGranulation")
private double sGranulation;

@Column(name = "sandKG")
private double sKg;

@Column(name = "sandLenght")
private double sLenght;

@Column(name = "sandWidth")
private double sWidth;

@Column(name = "sandHeight")
private double sHeight;

@Column(name = "sandDensity")
private double sDensity;

@Column(name = "sandVolume")
private double sVolume;

@OneToOne(fetch = FetchType.LAZY, optional = true)
@MapsId
private ReefTankOnlySandRocks reefTankOnlySandRocks;

@OneToOne(fetch = FetchType.LAZY, optional = true)
@MapsId
private ReefTankWithSump reefTankWithSump;


public Sand(long id, String sManufacturer, String sDescription, double sGranulation, double sKg, double sLenght,
        double sWidth, double sHeight, double sDensity, double sVolume, ReefTankOnlySandRocks reefTankOnlySandRocks,
        ReefTankWithSump reefTankWithSump) {
    super();
    this.id = id;
    this.sManufacturer = sManufacturer;
    this.sDescription = sDescription;
    this.sGranulation = sGranulation;
    this.sKg = sKg;
    this.sLenght = sLenght;
    this.sWidth = sWidth;
    this.sHeight = sHeight;
    this.sDensity = sDensity;
    this.sVolume = sVolume;
    this.reefTankOnlySandRocks = reefTankOnlySandRocks;
    this.reefTankWithSump = reefTankWithSump;
}

И после фабрики я пытаюсь построить объект для сохранения

ReefTankOnlySandRocks reefTank = (ReefTankOnlySandRocks) ReefTankFactory.createReefTank(cbAquariumTyp.getSelectionModel().getSelectedIndex());

        reefTank.setRtDescription(tfBezeichnung.getText().trim());
        reefTank.setRtCreatedOn(dpErstelltam.getValue());
        reefTank.setRtLength(aqVolumenBerechnen.getLaenge());
        reefTank.setRtWidth(aqVolumenBerechnen.getBreite());
        reefTank.setRtHeight(aqVolumenBerechnen.getHoehe());
        reefTank.setRtGrossVolume(aqVolumenBerechnen.getVolumenQuaderBerechnen());

        reefTank.setSand(sand);
        reefTank.setRock(rock);

        sand.setReefTankOnlySandRocks(reefTank);
        rock.setReefTankOnlySandRocks(reefTank);

        rock.setReefTankWithSump(null);
        sand.setReefTankWithSump(null);

Большое спасибо заранее

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...