我正在尝试创建一个名为 muziekstuk 的实体,其中包含名为 instrument 和 componist 的实体所以我创建了一个连接表,其中包含 muziekstuk 仪器和 componist 的 id 的 3 个外键。
muziekstuk 和乐器已创建,我从下拉列表中选择作曲家
这是操作代码:
MuziekstukDAOImpl reg = new MuziekstukDAOImpl();
ComponistDAOImpl com = new ComponistDAOImpl();
int componistid = Integer.parseInt(MComponist);
Componist componist = (Componist) com.getById(componistid);
Instrument i1 = new Instrument(MInstrument1, Integer.parseInt(MAantal1));
Muziekstuk muziekstuk = new Muziekstuk(MTitel,MGenre,componist,i1);
reg.save(muziekstuk);
它确实只在连接表中制作了一个新乐器和 muziekstuk,componist 的 id 为空:S 有人知道为什么吗?
这是我的其余代码: Muziekstuk:
@Entity
@Table(name = "STUD1630651.MFMUZIEKSTUK")
public class Muziekstuk {
@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="my_entity_seq_gen")
@SequenceGenerator(name="my_entity_seq_gen", sequenceName="HIBERNATE_SEQUENCE_MUZIEKSTUK")
@Column(name = "ID")
private int id;
@Column(name = "TITEL")
private String titel;
@Column(name = "GENRE")
private String genre;
@OneToOne(cascade=CascadeType.ALL)
@JoinTable(name="MFMUZIEKSTUKJOIN",joinColumns=
{@JoinColumn(name="MUZIEKSTUKID")},inverseJoinColumns=
{@JoinColumn(name="COMPONISTID")})
private Componist componist;
@OneToOne(cascade=CascadeType.ALL)
@JoinTable(name="MFMUZIEKSTUKJOIN",joinColumns=
{@JoinColumn(name="MUZIEKSTUKID")},inverseJoinColumns=
{@JoinColumn(name="INSTRUMENTID")})
private Instrument instrument;
public Muziekstuk(String titel, String genre, Componist componist, Instrument instrument){
this.titel = titel;
this.genre = genre;
this.componist = componist;
this.instrument = instrument;
}
public Muziekstuk(int id, String titel, String genre, Componist componist, Instrument instrument){
this.id = id;
this.titel = titel;
this.genre = genre;
this.componist = componist;
this.instrument = instrument;
}
//GETTERS AND SETTERS
作曲家:
@Entity
@Table(name = "STUD1630651.MFCOMPONIST")
public class Componist {
@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="my_entity_seq_gen")
@SequenceGenerator(name="my_entity_seq_gen", sequenceName="HIBERNATE_SEQUENCE_COMPONIST")
@Column(name = "ID")
private int id;
@Column(name = "NAAM")
private String naam;
@Column(name = "LAND")
private String land;
@Column(name = "TIJDVLAK")
private String tijdvlak;
@Column(name = "COMPOSITIES")
private String composities;
public Componist(int id, String naam, String land, String tijdvlak, String composities){
this.id = id;
this.naam = naam;
this.land = land;
this.tijdvlak = tijdvlak;
this.composities = composities;
}
public Componist(String naam, String land, String tijdvlak, String composities){
this.naam = naam;
this.land = land;
this.tijdvlak = tijdvlak;
this.composities = composities;
}
public Componist(){
}
//GETTERS AND SETTERS
乐器:
@Entity
@Table(name = "STUD1630651.MFCOMPONIST")
public class Componist {
@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="my_entity_seq_gen")
@SequenceGenerator(name="my_entity_seq_gen", sequenceName="HIBERNATE_SEQUENCE_COMPONIST")
@Column(name = "ID")
private int id;
@Column(name = "NAAM")
private String naam;
@Column(name = "LAND")
private String land;
@Column(name = "TIJDVLAK")
private String tijdvlak;
@Column(name = "COMPOSITIES")
private String composities;
public Componist(int id, String naam, String land, String tijdvlak, String composities){
this.id = id;
this.naam = naam;
this.land = land;
this.tijdvlak = tijdvlak;
this.composities = composities;
}
public Componist(String naam, String land, String tijdvlak, String composities){
this.naam = naam;
this.land = land;
this.tijdvlak = tijdvlak;
this.composities = composities;
}
public Componist(){
}
//GETTERS AND SETTERS
DAO 中的保存功能:
@Override
public void save(Object t) {
Muziekstuk muziekstuk = (Muziekstuk) t;
System.out.println(muziekstuk.getComponist().getId());
Session session = null;
Transaction transaction = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
transaction = session.beginTransaction();
session.saveOrUpdate(muziekstuk);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
System.out.println("exception");
e.printStackTrace();
}
} finally {
if (session != null) {
session.close();
}
}
}
这是 sql 跟踪:
Hibernate: select componist0_.ID as ID1_2_0_, componist0_.COMPOSITIES as COMPOSIT2_2_0_, componist0_.LAND as LAND3_2_0_, componist0_.NAAM as NAAM4_2_0_, componist0_.TIJDVLAK as TIJDVLAK5_2_0_ from STUD1630651.MFCOMPONIST componist0_ where componist0_.ID=?
Hibernate: select HIBERNATE_SEQUENCE_MUZIEKSTUK.nextval from dual
Hibernate: select HIBERNATE_SEQUENCE_INSTRUMENT.nextval from dual
Hibernate: insert into STUD1630651.MFINSTRUMENT (HOEVEELHEID, NAAM, ID) values (?, ?, ?)
Hibernate: insert into STUD1630651.MFMUZIEKSTUK (GENRE, TITEL, ID) values (?, ?, ?)
Hibernate: insert into MFMUZIEKSTUKJOIN (INSTRUMENTID, MUZIEKSTUKID) values (?, ?)
那应该是 3 ?在 MUZIEKSTUKJOIN