我正在尝试使用名为instrument and componist的实体创建一个名为muziekstuk的实体 所以我创建了一个带有3个外键的连接表,用于muziekstuk乐器和组件的id。
创建了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为null: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
Componist:
@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 trace:
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
答案 0 :(得分:0)
事实证明,连接表只能引用1个表而不是2个,所以如果有人在想,你应该制作2个连接表。