您好,我在Lawyer表中有多对一关系单向,我使用 @jointable 注释来创建第三个表,所以当我插入律师相应的国家城市和地区ID应插入第三表。
但问题是只有state_code and lawyer_batch_no
正在插入,并且列的其余部分未插入第三个表中。
虽然我已经在律师对象中设置了每个依赖依赖。
这是查询被解雇
Hibernate: select state_.state_code, state_.abbreviation as abbrevia2_18_, state_.COUNTRY as COUNTRY4_18_, state_.NAME as NAME3_18_ from state state_ where state_.state_code=?
02:12:42,527 TRACE BasicBinder:81 - binding parameter [1] as [VARCHAR] - [UP]
02:12:42,576 TRACE BasicExtractor:78 - extracted value ([abbrevia2_18_] : [VARCHAR]) - [UP]
02:12:42,576 TRACE BasicExtractor:78 - extracted value ([COUNTRY4_18_] : [VARCHAR]) - [IND]
02:12:42,577 TRACE BasicExtractor:78 - extracted value ([NAME3_18_] : [VARCHAR]) - [Uttar Pradesh]
Hibernate: insert into lawyer (court_of_practise, date_of_birth, experience_summary_id, first_name, last_name, user_id) values (?, ?, ?, ?, ?, ?)
02:12:42,588 TRACE BasicBinder:69 - binding parameter [1] as [VARCHAR] - [null]
02:12:42,588 TRACE BasicBinder:81 - binding parameter [2] as [TIMESTAMP] - [Wed Dec 14 02:12:42 IST 2016]
02:12:42,589 TRACE BasicBinder:81 - binding parameter [3] as [INTEGER] - [4]
02:12:42,589 TRACE BasicBinder:81 - binding parameter [4] as [VARCHAR] - [sofi]
02:12:42,590 TRACE BasicBinder:81 - binding parameter [5] as [VARCHAR] - [gupta]
02:12:42,590 TRACE BasicBinder:81 - binding parameter [6] as [BIGINT] - [4]
Hibernate: insert into lawyer_lscd (state_code, lawyer_batch_no) values (?, ?)
02:12:42,596 TRACE BasicBinder:81 - binding parameter [1] as [VARCHAR] - [UP]
02:12:42,596 TRACE BasicBinder:81 - binding parameter [2] as [INTEGER] - [4]
这是律师pozo
@Entity
@Table(name = "lawyer")
class Lawyer{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "lawyer_batch_no")
private int lawyerbatchNo;
@ManyToOne(targetEntity = Country.class, optional = true)
@JoinTable(name = "lawyer_lscd", joinColumns = @JoinColumn(name = "lawyer_batch_no",unique=true), inverseJoinColumns = @JoinColumn(name = "country_code"))
private Country country;
@ManyToOne(targetEntity = State.class, optional = true)
@JoinTable(name = "lawyer_lscd", joinColumns = @JoinColumn(name = "lawyer_batch_no",unique=true), inverseJoinColumns = @JoinColumn(name = "state_code"))
private State state;
@ManyToOne(targetEntity = City.class, optional = true)
@JoinTable(name = "lawyer_lscd", joinColumns = @JoinColumn(name = "lawyer_batch_no",unique=true), inverseJoinColumns = @JoinColumn(name = "city_code"))
private City city;
@ManyToOne(targetEntity = District.class, optional = true)
@JoinTable(name = "lawyer_lscd", joinColumns = @JoinColumn(name = "lawyer_batch_no",unique=true), inverseJoinColumns = @JoinColumn(name = "district_code"))
private District district;
...........................................
getter setter
}
这是我尝试将国家/地区城市和地区设置为律师对象的主要方法
Lawyer lawyer = new Lawyer();
lawyer.setFirstName("sofi");
lawyer.setLastName("gupta");
lawyer.setDateOfBirth(new Date());
Country country = new Country();
country.setCountry_code("IND");
country.setAbbreviation("IND");
country.setName("India");
State state = new State();
state.setState_code("UP");
state.setAbbreviation("UP");
state.setName("Uttar Pradesh");
state.setCountry(country);
City city = new City();
city.setCity_code("BNDA");
city.setCityName("Banda");
city.setState(state);
City city2 = new City();
city2.setCity_code("LKO");
city2.setCityName("Lucknow");
city2.setState(state);
District district = new District();
district.setDistrict_code("MUA");
district.setName("MAHUA");
district.setCity(city);
District district1 = new District();
district1.setDistrict_code("MAHOBA");
district1.setName("MAHOBA");
district1.setCity(city);
city.getDistricts().add(district);
city.getDistricts().add(district1);
state.getCity().add(city);
state.getCity().add(city2);
country.getState().add(state);
lawyer.setCountry(country);
lawyer.setState(state);
lawyer.setCity(city);
lawyer.setDistrict(district);
LawyerDao clawDao = (LawyerDaoImpl) applicationContext.getBean("lawyerDaoImpl");
clawDao.saveLayer(lawyer);
这是我在dao类中的保存方法
public Lawyer saveLayer(Lawyer lawyer) {
Session session = sessionFactory.openSession();
org.hibernate.Transaction tran = session.beginTransaction();
int lawyerId = 0;
if (session.isOpen()) {
lawyerId = (Integer) session.save(lawyer);
tran.commit();
session.flush();
}
Lawyer savedLawyer = (Lawyer) session.get(Lawyer.class, lawyerId);
session.close();
return savedLawyer;
}