码迷,mamicode.com
首页 > 数据库 > 详细

SSH框架之Hibernate数据库外键如何插入值的问题

时间:2015-06-03 15:47:19      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:ssh

SSH框架之Hibernate数据库外键如何插入值的问题

一、目标:

现有表:

技术分享

其中Tea_id属于外键,如何向含有外键的表中插入对应的数据。

二、pojos部分

Course_information .java

package com.pojos;


import java.util.Date;


public class Course_information {
private Integer Course_id;
private String Course_name;
private Integer Course_level;
private String Course_sign_begin;
private String Course_sign_end;
private Date Course_add_time;
private Integer Course_option_numer;
private String Course_message;
/*关联教师*/
private Teacher_information teacher_information;
public Course_information() {
super();
}
/*构造函数*/
public Course_information(Integer Course_id,String Course_name,Teacher_information teacher_information,
Integer Course_level,String Course_sign_begin,String Course_sign_end,Date Course_add_time,
String Course_message){
this.Course_id = Course_id;
this.Course_name = Course_name;
this.teacher_information = teacher_information;
this.Course_level = Course_level;
this.Course_sign_begin = Course_sign_begin;
this.Course_sign_end = Course_sign_end;
this.Course_add_time = Course_add_time;
this.Course_message = Course_message;
}
public Integer getCourse_id() {
return Course_id;
}
public void setCourse_id(Integer courseId) {
Course_id = courseId;
}
public String getCourse_name() {
return Course_name;
}
public void setCourse_name(String courseName) {
Course_name = courseName;
}
public Integer getCourse_level() {
return Course_level;
}
public void setCourse_level(Integer courseLevel) {
Course_level = courseLevel;
}
public String getCourse_sign_begin() {
return Course_sign_begin;
}
public void setCourse_sign_begin(String courseSignBegin) {
Course_sign_begin = courseSignBegin;
}
public String getCourse_sign_end() {
return Course_sign_end;
}
public void setCourse_sign_end(String courseSignEnd) {
Course_sign_end = courseSignEnd;
}


public Date getCourse_add_time() {
return Course_add_time;
}


public void setCourse_add_time(Date courseAddTime) {
Course_add_time = courseAddTime;
}


public Integer getCourse_option_numer() {
return Course_option_numer;
}
public void setCourse_option_numer(Integer courseOptionNumer) {
Course_option_numer = courseOptionNumer;
}

public String getCourse_message() {
return Course_message;
}
public void setCourse_message(String courseMessage) {
Course_message = courseMessage;
}
public Teacher_information getTeacher_information() {
return teacher_information;
}
public void setTeacher_information(Teacher_information teacherInformation) {
teacher_information = teacherInformation;
}
}

Course_information.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.pojos">
<class name="Course_information" table="course_information">
<!-- 课程编号 -->
<id name="Course_id" type="java.lang.Integer">
<column name="Course_id"/>
<generator class="native"/>
</id>
<!-- 课程名称 -->
<property column="Course_name" generated="never" lazy="false"
name="Course_name" type="java.lang.String" length="40" not-null="true"/>

<!-- 任课教师ID -->
<many-to-one name="teacher_information" column="Tea_id" class="Teacher_information"></many-to-one>

<!-- 课程性质 -->
<property column="Course_level" generated="never" lazy="false" not-null="true"
name="Course_level" type="java.lang.Integer"/>

<!-- 签到时间上限 -->
<property column="Course_sign_begin" generated="never" lazy="false" not-null="false"
name="Course_sign_begin" type="java.lang.String" length="20"/>

<!-- 签到时间下限 -->
<property column="Course_sign_end" generated="never" lazy="false" not-null="false"
name="Course_sign_end" type="java.lang.String" length="20"/>

<!-- 课程添加时间 -->
<property column="Course_add_time" generated="never" lazy="false" not-null="false"
name="Course_add_time" type="java.util.Date"/>

<!-- 课程选择总人数 -->
<property column="Course_option_numer" generated="never" lazy="false" not-null="false"
name="Course_option_numer" type="java.lang.Integer"/>

<!-- 课程描述 -->
<property column="Course_message" generated="never" lazy="false"
name="Course_message" type="java.lang.String"/>

</class>
</hibernate-mapping>

、action业务逻辑层部分

CourseManagerAction .java

package com.teacher.web.action;
import java.util.Date;
import com.pojos.Course_information;
import com.pojos.Teacher_information;
import com.teacher.comm.BaseAction;
import com.teacher.service.CourseService;
import com.teacher.service.Teacher_informationService;


@SuppressWarnings("serial")
public class CourseManagerAction extends BaseAction{
/*定义所有属性*/
private Integer Course_id;
private String Course_name;
private Integer Course_level;
private String Course_sign_begin;
private String Course_sign_end;
private Date Course_add_time;
private Integer Course_option_numer;
private String Course_message;

/*定义教师表对象  记录当前教师的工号和教师编号*/
private Integer Tea_id;
private String Tea_number;

private Course_information course_information;

/*定义服务层  接口类对象*/
private Teacher_informationService teacher_informationService;

private CourseService courseService;

/*进入添加课程页面*/
public String add_course(){
System.out.println("当前的教师工号:"+Tea_number);
return "add_course";
}
public String save_new_course(){
try{
Course_information course = new Course_information();
course.setCourse_name(Course_name);
System.out.println("当前的教师工号:"+Tea_number);
Teacher_information tea = teacher_informationService.findBy_Tea_number(Tea_number);
System.out.println("获取到的当前教师编号为:"+tea.getTea_id());
course.setTeacher_information(tea);
course.setCourse_level(Course_level);
course.setCourse_sign_begin(Course_sign_begin);
course.setCourse_sign_end(Course_sign_end);
course.setCourse_add_time(new Date());
course.setCourse_message(Course_message);
courseService.saveOneCourse(course);
}catch(Exception e){
System.out.println(e.toString());
}
return "add_course_ok";
}

public Integer getCourse_id() {
return Course_id;
}
public void setCourse_id(Integer courseId) {
Course_id = courseId;
}
public String getCourse_name() {
return Course_name;
}
public void setCourse_name(String courseName) {
Course_name = courseName;
}
public Integer getCourse_level() {
return Course_level;
}
public void setCourse_level(Integer courseLevel) {
Course_level = courseLevel;
}

public Date getCourse_add_time() {
return Course_add_time;
}
public void setCourse_add_time(Date courseAddTime) {
Course_add_time = courseAddTime;
}
public String getCourse_message() {
return Course_message;
}
public void setCourse_message(String courseMessage) {
Course_message = courseMessage;
}
public CourseService getCourseService() {
return courseService;
}
public void setCourseService(CourseService courseService) {
this.courseService = courseService;
}
public Integer getTea_id() {
return Tea_id;
}


public void setTea_id(Integer teaId) {
Tea_id = teaId;
}


public String getTea_number() {
return Tea_number;
}


public void setTea_number(String teaNumber) {
Tea_number = teaNumber;
}


public Teacher_informationService getTeacher_informationService() {
return teacher_informationService;
}


public void setTeacher_informationService(
Teacher_informationService teacherInformationService) {
teacher_informationService = teacherInformationService;
}
public String getCourse_sign_begin() {
return Course_sign_begin;
}
public void setCourse_sign_begin(String courseSignBegin) {
Course_sign_begin = courseSignBegin;
}
public String getCourse_sign_end() {
return Course_sign_end;
}
public void setCourse_sign_end(String courseSignEnd) {
Course_sign_end = courseSignEnd;
}
public Integer getCourse_option_numer() {
return Course_option_numer;
}
public void setCourse_option_numer(Integer courseOptionNumer) {
Course_option_numer = courseOptionNumer;
}
public Course_information getCourse_information() {
return course_information;
}
public void setCourse_information(Course_information courseInformation) {
course_information = courseInformation;
}
}

、数据实现层daoHib

CourseDaoHib .java

package com.teacher.daoHib;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.pojos.Course_information;
import com.teacher.dao.CourseDao;

public class CourseDaoHib extends HibernateDaoSupport implements CourseDao{
/*保存页面添加的课程信息*/
@Override
public void saveOneCourse(Course_information course) {
getHibernateTemplate().save(course);
}
}

五、applicationContext-teacher.xml(部分代码)

特别注意,如果action业务逻辑层需要调用其它层已经写好的方法,需要此配置文件中添加对应的Service层参照,否则方法将不能调用。

<!-- 课程表 -->
<!-- Dao -->
<bean id="courseDao" class="com.teacher.daoHib.CourseDaoHib">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- Service -->
<bean id="courseService" class="com.teacher.serviceImpl.CourseServiceImpl">
<property name="courseDao" ref="courseDao"></property>
</bean>

<!--教师 Action -->
<bean id="courseManagerAction" class="com.teacher.web.action.CourseManagerAction"
scope="prototype">
<property name="courseService" ref="courseService"></property>
<property name="teacher_informationService" ref="teacher_informationService"></property>
</bean>

六、实现效果

技术分享

以上,仅供以后SSH开发参考使用。

SSH框架之Hibernate数据库外键如何插入值的问题

标签:ssh

原文地址:http://blog.csdn.net/u014344668/article/details/46345327

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!