mysql中的3个表格如下:
CREATE TABLE tstudent(
id varchar(36) primary key not null,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
grade int NOT NULL,
sex VARCHAR(1) NOT NULL,
birthday DATE,
dayorboarding VARCHAR(1) NOT NULL,
age int ,
house VARCHAR(20),
mentor VARCHAR(50)
);
CREATE TABLE ttrip(
id varchar(36) primary key not null,
departureday DATE NOT NULL,
name varchar(100) not null unique
);
CREATE TABLE tsignup(
id varchar(36) primary key not null,
s_id varchar(36) not null,
FOREIGN KEY (s_id) REFERENCES tstudent(id),
t_id varchar(36) not null,
FOREIGN KEY (t_id) REFERENCES ttrip(id),
departuretransport VARCHAR(1) NOT NULL,
returntransport VARCHAR(1) NOT NULL,
remark TEXT
);
tstudent模特喜欢这个:
package cz.model;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* Tstudent entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "tstudent", catalog = "rlcbau")
public class Tstudent implements java.io.Serializable {
// Fields
private String id;
private String firstname;
private String lastname;
private Integer grade;
private String sex;
private Date birthday;
private String dayorboarding;
private Integer age;
private String house;
private String mentor;
private Set<Tsignup> tsignups = new HashSet<Tsignup>(0);
// Constructors
/** default constructor */
public Tstudent() {
}
/** minimal constructor */
public Tstudent(String id, String firstname, String lastname, Integer grade, String sex, String dayorboarding) {
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.grade = grade;
this.sex = sex;
this.dayorboarding = dayorboarding;
}
/** full constructor */
public Tstudent(String id, String firstname, String lastname, Integer grade, String sex, Date birthday, String dayorboarding, Integer age, String house, String mentor, Set<Tsignup> tsignups) {
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.grade = grade;
this.sex = sex;
this.birthday = birthday;
this.dayorboarding = dayorboarding;
this.age = age;
this.house = house;
this.mentor = mentor;
this.tsignups = tsignups;
}
// Property accessors
@Id
@Column(name = "id", unique = true, nullable = false, length = 36)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
@Column(name = "firstname", nullable = false, length = 30)
public String getFirstname() {
return this.firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
@Column(name = "lastname", nullable = false, length = 30)
public String getLastname() {
return this.lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
@Column(name = "grade", nullable = false)
public Integer getGrade() {
return this.grade;
}
public void setGrade(Integer grade) {
this.grade = grade;
}
@Column(name = "sex", nullable = false, length = 1)
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Temporal(TemporalType.DATE)
@Column(name = "birthday", length = 10)
public Date getBirthday() {
return this.birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Column(name = "dayorboarding", nullable = false, length = 1)
public String getDayorboarding() {
return this.dayorboarding;
}
public void setDayorboarding(String dayorboarding) {
this.dayorboarding = dayorboarding;
}
@Column(name = "age")
public Integer getAge() {
return this.age;
}
public void setAge(Integer age) {
this.age = age;
}
@Column(name = "house", length = 20)
public String getHouse() {
return this.house;
}
public void setHouse(String house) {
this.house = house;
}
@Column(name = "mentor", length = 50)
public String getMentor() {
return this.mentor;
}
public void setMentor(String mentor) {
this.mentor = mentor;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "tstudent")
public Set<Tsignup> getTsignups() {
return this.tsignups;
}
public void setTsignups(Set<Tsignup> tsignups) {
this.tsignups = tsignups;
}
}
ttrip模特喜欢这个:
package cz.model;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;
/**
* Ttrip entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "ttrip", catalog = "rlcbau", uniqueConstraints = @UniqueConstraint(columnNames = "name"))
public class Ttrip implements java.io.Serializable {
// Fields
private String id;
private String departureday;
private String name;
private Set<Tsignup> tsignups = new HashSet<Tsignup>(0);
// Constructors
/** default constructor */
public Ttrip() {
}
/** minimal constructor */
public Ttrip(String id, String departureday, String name) {
this.id = id;
this.departureday = departureday;
this.name = name;
}
/** full constructor */
public Ttrip(String id, String departureday, String name, Set<Tsignup> tsignups) {
this.id = id;
this.departureday = departureday;
this.name = name;
this.tsignups = tsignups;
}
// Property accessors
@Id
@Column(name = "id", unique = true, nullable = false, length = 36)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
@Column(name = "departureday", nullable = false, length = 10)
public String getDepartureday() {
return this.departureday;
}
public void setDepartureday(String departureday) {
this.departureday = departureday;
}
@Column(name = "name", unique = true, nullable = false, length = 100)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "ttrip")
public Set<Tsignup> getTsignups() {
return this.tsignups;
}
public void setTsignups(Set<Tsignup> tsignups) {
this.tsignups = tsignups;
}
}
tsignup模特喜欢这个:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* Tsignup entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "tsignup", catalog = "rlcbau")
public class Tsignup implements java.io.Serializable {
// Fields
private String id;
private Ttrip ttrip;
private Tstudent tstudent;
private String departuretransport;
private String returntransport;
private String remark;
// Constructors
/** default constructor */
public Tsignup() {
}
/** minimal constructor */
public Tsignup(String id, Ttrip ttrip, Tstudent tstudent, String departuretransport, String returntransport) {
this.id = id;
this.ttrip = ttrip;
this.tstudent = tstudent;
this.departuretransport = departuretransport;
this.returntransport = returntransport;
}
/** full constructor */
public Tsignup(String id, Ttrip ttrip, Tstudent tstudent, String departuretransport, String returntransport, String remark) {
this.id = id;
this.ttrip = ttrip;
this.tstudent = tstudent;
this.departuretransport = departuretransport;
this.returntransport = returntransport;
this.remark = remark;
}
// Property accessors
@Id
@Column(name = "id", unique = true, nullable = false, length = 36)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "t_id", nullable = false)
public Ttrip getTtrip() {
return this.ttrip;
}
public void setTtrip(Ttrip ttrip) {
this.ttrip = ttrip;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "s_id", nullable = false)
public Tstudent getTstudent() {
return this.tstudent;
}
public void setTstudent(Tstudent tstudent) {
this.tstudent = tstudent;
}
@Column(name = "departuretransport", nullable = false, length = 1)
public String getDeparturetransport() {
return this.departuretransport;
}
public void setDeparturetransport(String departuretransport) {
this.departuretransport = departuretransport;
}
@Column(name = "returntransport", nullable = false, length = 1)
public String getReturntransport() {
return this.returntransport;
}
public void setReturntransport(String returntransport) {
this.returntransport = returntransport;
}
@Column(name = "remark", length = 65535)
public String getRemark() {
return this.remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
我有&#34; signupAction&#34;喜欢这个:
public void save_stulst(){
Json j = new Json();
try {
// logger.info(signup.getId()+signup.getS_id()+signup.getT_id()+signup.getRemark()+signup.getDeparturetransport()+signup.getReturntransport());
signupService.save_stulst(signup);
j.setSuccess(true);
j.setMsg("Signup succeeds!");
} catch (Exception e) {
j.setMsg(e.getMessage());
}
super.writeJson(j);
}
在服务级别,我有&#34; signupService&#34;:
@Service(value="signupService")
public class SignupServiceImpl implements SignupServiceI {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(StudentServiceImpl.class);
private BaseDaoI<Tsignup> signupDao;
private BaseDaoI<Tstudent> studentDao;
private BaseDaoI<Ttrip> tripDao;
public BaseDaoI<Tsignup> getSignupDao() {
return signupDao;
}
@Autowired
public void setSignupDao(BaseDaoI<Tsignup> signupDao) {
this.signupDao = signupDao;
}
public BaseDaoI<Ttrip> getTripDao() {
return tripDao;
}
@Autowired
public void setTripDao(BaseDaoI<Ttrip> tripDao) {
this.tripDao = tripDao;
}
public BaseDaoI<Tstudent> getStudentDao() {
return studentDao;
}
@Autowired
public void setStudentDao(BaseDaoI<Tstudent> studentDao) {
this.studentDao = studentDao;
}
@Override
public void save_stulst(Signup signup) {
Tsignup tsignup = new Tsignup();
Tstudent tstudent = studentDao.get(Tstudent.class, signup.getS_id());
Ttrip ttrip = tripDao.get(Ttrip.class, signup.getT_id());
BeanUtils.copyProperties(signup, tsignup, new String[]{"id"});
tsignup.setId(UUID.randomUUID().toString());
tsignup.setTstudent(tstudent);
tsignup.setTtrip(ttrip);
signupDao.save(tsignup);
}
这是baseDaoImpl: package cz.dao.impl;
import org.apache.log4j.Logger;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import cz.dao.BaseDaoI;
@Repository("baseDao")
public class BaseDaoImpl<T> implements BaseDaoI<T> {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(BaseDaoImpl.class);
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private Session getCurrentSession(){
return this.sessionFactory.getCurrentSession();
}
@Override
public Serializable save(T o) {
return this.getCurrentSession().save(o);
}
@Override
public T get(Class<T> c, Serializable id ) {
return (T) this.getCurrentSession().get(c, id);
}
@Override
public T get(String hql) {
Query q = this.getCurrentSession().createQuery(hql);
List<T> l = q.list();
if (l!=null && l.size()>0){
return l.get(0);
}else{
return null;
}
}
@Override
public T get(String hql, Map<String, Object> params) {
Query q = this.getCurrentSession().createQuery(hql);
if(params != null && !params.isEmpty()){
for (String key : params.keySet()){
q.setParameter(key, params.get(key));
}
}
List<T> l = q.list();
if (l!=null && l.size()>0){
return l.get(0);
}else{
return null;
}
}
@Override
public void delete(T o) {
this.getCurrentSession().delete(o);
}
@Override
public void update(T o) {
this.getCurrentSession().update(o);
}
@Override
public void saveOrUpdate(T o) {
this.getCurrentSession().save(o);
}
@Override
public List<T> find(String hql) {
Query q = this.getCurrentSession().createQuery(hql);
return q.list();
}
@Override
public List<T> find(String hql, Map<String, Object> params) {
Query q = this.getCurrentSession().createQuery(hql);
if(params != null && !params.isEmpty()){
for (String key : params.keySet()){
q.setParameter(key, params.get(key));
}
}
return q.list();
}
@Override
public List<T> find(String hql, Map<String, Object> params, int page, int rows) {
Query q = this.getCurrentSession().createQuery(hql);
if(params != null && !params.isEmpty()){
for (String key : params.keySet()){
q.setParameter(key, params.get(key));
}
}
return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
}
@Override
public List<T> find(String hql, int page, int rows) {
Query q = this.getCurrentSession().createQuery(hql);
return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
}
@Override
public Long count(String hql) {
Query q = this.getCurrentSession().createQuery(hql);
return (Long) q.uniqueResult();
}
@Override
public Long count(String hql, Map<String, Object> params) {
Query q = this.getCurrentSession().createQuery(hql);
if(params != null && !params.isEmpty()){
for (String key : params.keySet()){
q.setParameter(key, params.get(key));
}
}
return (Long) q.uniqueResult();
}
@Override
public int executeHql(String hql) {
Query q = this.getCurrentSession().createQuery(hql);
return q.executeUpdate();
}
}
问题:当我尝试从网上保存时,我收到这样的错误:
[org.hibernate.engine.jdbc.spi.SqlExceptionHelper]SQL Error: 1048, SQLState: 23000
[org.hibernate.engine.jdbc.spi.SqlExceptionHelper]Column 's_id' cannot be null
[org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl]HHH000010: On release of batch it still contained JDBC statements
我是hibernate的新手,不知道哪一步是错的。我已经给ttrip和ttrudent进入了tsignup模型..应该已经保存了
答案 0 :(得分:0)
你对“tsignup”表的定义清楚地显示了“s_id”列的NOT NULL约束。你真的需要这个吗? 如果不删除它,这应该工作
干杯 了Anant
答案 1 :(得分:0)
更改您的tsignup
表格
CREATE TABLE tsignup(
...
s_id varchar(36) not null,
...
要
CREATE TABLE tsignup(
...
s_id VARCHAR(36) NULL,
...