There are the following entities:
@Entity public class City { public City() { } public City(String name, Country country, String district, int population) { this.name = name; this.country = country; this.district = district; this.population = population; } @Id @SequenceGenerator(name = "city_id_sequence", sequenceName = "city_id_sequence", allocationSize = 1) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "city_id_sequence") private int id; @OneToOne(optional =false, mappedBy = "capital") private Country country; private String name; private String district; private int population; //geters and setters } and
@Entity @Table(name = "country") public class Country { public Country() { } public Country(String code, String name, String continent, String region, double area, int year, int population, double lifeExp, double gnp, double gnpOld, String localName, String governmentForm, String headOfState, City capital, String code2) { this.code = code; this.name = name; this.continent = continent; this.region = region; this.area = area; this.year = year; this.population = population; this.lifeExp = lifeExp; this.gnp = gnp; this.gnpOld = gnpOld; this.localName = localName; this.governmentForm = governmentForm; this.headOfState = headOfState; this.capital = capital; this.code2 = code2; } @Id @Column(name = "code") private String code; @Column(name = "name") private String name; @Column(name = "continent") private String continent; @Column(name = "region") private String region; @Column(name = "surfacearea") private double area; @Column(name = "indepyear") private int year; @Column(name = "population") private int population; @Column(name = "lifeexpectancy") private double lifeExp; @Column(name = "gnp") private double gnp; @Column(name = "gnpold") private double gnpOld; @Column(name = "localname") private String localName; @Column(name = "governmentform") private String governmentForm; @Column(name = "headofstate") private String headOfState; @OneToOne(optional = false) @JoinColumn(name = "capital") private City capital; //внимание сюда!!! @Column(name = "code2") private String code2; apparently the second refers to the first. And there are no problems with the Country essence. All fields in the table are filled. The problem is in the first City entity, and specifically with the country field - it is not displayed in any way in the table, that is, the country substitution is not specified in the query generated by the hibernate. Sql query fragment:
Hibernate: insert into City (district, name, population, id) values (?, ?, ?, ?) that is, the country parameter is simply not enough. And since it is in the table, the following crashes out: a mole saying zero in the field hasnoted. Most likely there is not enough annotation.
City is going as follows:
void createCity(City city,String code){ manager.getTransaction().begin(); city.setCountry(manager.find(Country.class,code)); manager.persist(city); manager.getTransaction().commit(); } String code - This is the code of the country which will belong to this city. According to him, the country is taken from the base and is substituted specifically in this city.