I am trying to map the Student entity to several tables: students , addresses , hobbies . The keys in the addresses and hobbies are also external and refer to the primary key of the students table. That is, the table of students basic, and the rest are auxiliary. Here are the class descriptions:
Student
package entity; import javax.persistence.*; @Entity @Table(name = "students") @SecondaryTables({ @SecondaryTable(name = "hobbies"), @SecondaryTable(name = "addresses"), }) public class Student { @Id @Column(name = "student_id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @Column(table = "hobbies") private String hobby; @Column(table = "addresses") @Embedded private Address address; public Student(){} public Student(String name, String hobby, Address address){ this.name = name; this.hobby = hobby; this.address = address; } } Address
package entity; import javax.persistence.*; @Embeddable public class Address { @Column(name="street") private String street; public Address(){} public Address(String street){ this.street = street; } } As a result, the String hobby field of the Student class is mapped into a separate hobbies table without problems, but the Address address cannot be displayed. That is, the addresses table is created, and the foreign key is registered in it, but the data is not received (the table is empty). In addition, there is only one field in the addresses table - student_id , and for some reason there is no street field.
It creates data like this:
public static void main(String[] args) { StudentService service = new StudentService(); try { Student student = service.create( new Student("Вася", "Лыжи", new Address("ул. Ленина")) ); Student student2 = service.create( new Student("Петя", "Хоккей", new Address("ул. Фрунзе")) ); } catch (DBException e){ System.out.println("что-то пошло не так"); } DBService.close(); } This is a service layer:
package service; import dao.*; import exception.DBException; import entity.Student; import org.hibernate.HibernateException; import org.hibernate.Transaction; import javax.persistence.NoResultException; public class StudentService { public StudentService(){} public Student create(Student student) throws DBException { Transaction transaction = DBService.getTransaction(); try { StudentDao studentDao = DaoFactory.getStudentDao(); Long studentId = studentDao.create(student); student = studentDao.get(studentId); transaction.commit(); return student; } catch (HibernateException | NoResultException | NullPointerException e) { DBService.transactionRollback(transaction); throw new DBException(e); } } } Why is the addresses table empty and how to make it so that it has the correct structure (it has a street field) and it is filled with data? There are no warnings or errors when executing (but there are no requests to insert data into the addresses table):
Hibernate: drop table addresses if exists Hibernate: drop table hobbies if exists Hibernate: drop table students if exists Hibernate: create table addresses (student_id bigint not null, primary key (student_id)) Hibernate: create table hobbies (hobby varchar(255), student_id bigint not null, primary key (student_id)) Hibernate: create table students (student_id bigint generated by default as identity, street varchar(255), name varchar(255), primary key (student_id)) Hibernate: alter table addresses add constraint FKqq1nt3g94upydb2qt72xhnk7y foreign key (student_id) references students Hibernate: alter table hobbies add constraint FKmcdfc4qw94xl2a8ak5kct6rys foreign key (student_id) references students мар 15, 2019 5:43:34 PM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@3d9f6567' Hibernate: insert into students (student_id, street, name) values (null, ?, ?) мар 15, 2019 5:43:34 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop INFO: HHH10001008: Cleaning up connection pool [jdbc:h2:./h2db] Hibernate: insert into hobbies (hobby, student_id) values (?, ?) Hibernate: select student_id from students where student_id =? for update Hibernate: insert into students (student_id, street, name) values (null, ?, ?) Hibernate: insert into hobbies (hobby, student_id) values (?, ?) Hibernate: select student_id from students where student_id =? for update Process finished with exit code 0
@Embeddable, but to connect through one-to-one communication.@Embeddedsays that in Java code they are two different entities, but there is one table in the database. - Tsyklophobbyfield easily displayed in a separate table without any one-to-one? If you make a list or map display of data labeled@Embeddable, then the new table and the data in it are created without problems. How do you fix the classes in your opinion? - golubtsoffhobbyfield has no@Embeddedannotation. try adding it and see what happens. Or as the person above suggests. - Tsyklop@Embeddedno difference@Embeddedin front of the address, or not. I note that the Address class must be marked with the @Embeddable annotation, otherwise you will have to prescribe the entity in the manager configuration with all the ensuing consequences. - golubtsoff