2 qq 35973977 qq_35973977 于 2016.09.13 19:45 提问

SSH整合框架中save方法执行的时候会删除原有数据

SSH整合框架中save方法执行的时候会删除原有数据
如:
Hibernate: delete from stu_tea where tea_id=?
Hibernate: insert into stu_tea (tea_id, stu_id) values (?, ?)

我用的实体类注解方式,多对多关联关系,学生和老师类,执行save的时候每次中间表的关联关系都会被新的关联关系覆盖,看sql语句输出是现实先把原来的删除了,再添加的,检查过自己的配置update这个确定是update不是create;

学生类

 package com.cdd.ssh.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity
public class Students {
    @Id
    @Column(name="sId")//可省略
    //设置主键自增
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int sId;
    private String sName;
    private String sSex;
    private int sAge;
    @ManyToMany(mappedBy="students",cascade={CascadeType.PERSIST})
    private Set<Teachers> teachers = new HashSet<Teachers>();


    public int getsId() {
        return sId;
    }
    public void setsId(int sId) {
        this.sId = sId;
    }
    public String getsName() {
        return sName;
    }
    public void setsName(String sName) {
        this.sName = sName;
    }
    public String getsSex() {
        return sSex;
    }
    public void setsSex(String sSex) {
        this.sSex = sSex;
    }
    public int getsAge() {
        return sAge;
    }
    public void setsAge(int sAge) {
        this.sAge = sAge;
    }
    public Set<Teachers> getTeachers() {
        return teachers;
    }
    public void setTeachers(Set<Teachers> teachers) {
        this.teachers = teachers;
    };
}

老师类

 package com.cdd.ssh.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
//注明实体类对应表明,省略默认使用类名
@Table(name="teachers")
public class Teachers {
    //表明id列
    @Id
    @Column(name="tId")//可省略
    //设置主键自增
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int tId;
    private String tType;//科目
    private String tName;//姓名
    @ManyToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE}) 
    //设置中间表关联关系
    @JoinTable(name="stu_tea",
            joinColumns ={@JoinColumn(name = "tea_id")},
            inverseJoinColumns={@JoinColumn(name="stu_id")})            
    private Set<Students> students = new HashSet<Students>();


    public int gettId() {
        return tId;
    }
    public void settId(int tId) {
        this.tId = tId;
    }
    public String gettType() {
        return tType;
    }
    public void settType(String tType) {
        this.tType = tType;
    }
    public String gettName() {
        return tName;
    }
    public void settName(String tName) {
        this.tName = tName;
    }
    public Set<Students> getStudents() {
        return students;
    }
    public void setStudents(Set<Students> students) {
        this.students = students;
    }


}

这个是dao中的执行方法

 public void add(Students student, Teachers teacher) {
        // TODO Auto-generated method stub
        Session session = this.getSession();
        student.getTeachers().add(teacher);
        teacher.getStudents().add(student);
        session.save(teacher);
        session.save(student);
        System.out.println(teacher.gettId());
        System.out.println(student.getsId());
    }

这是我的代码,求大神帮忙啊,一下午了都没弄好,

在这之前我是写的学生和教室类,后来感觉教室不合适,就把教室改成老师类了,配置文件和属性都没有改变,只把用到教室的地方换成了老师;

求大神帮忙

这里是applicationContext.xml的全部配置

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
">

      <!-- 开启注解 -->
    <context:annotation-config/>

    <!-- 扫描包设置 -->
    <context:component-scan base-package="com.cdd.ssh.*"></context:component-scan>

    <!--DateSource数据源  使用DBCP连接池-->
    <bean id="dataSource"  class="org.apache.commons.dbcp.BasicDataSource">  
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
        <property name="url" value="jdbc:mysql://localhost:3306/school?useUnicode=true&amp;characterEncoding=UTF8"/>  
        <property name="username" value="root"/>  
        <property name="password" value="chengzi"/>  
    </bean>


    <bean id="sessionFactory"   
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>

    <!-- 载入实体类      mappingResources -->
    <property name="packagesToScan">
            <value>com.cdd.ssh.entity</value>
        </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>

    </bean>

    <!--  3.~~~~~~~~~~~~~~~~~~~~~~~~~事务配置~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
    <bean id="txManager" 
        class="org.springframework.orm.hibernate3.HibernateTransactionManager" > 
    <property name="sessionFactory" ref="sessionFactory"></property>         
    </bean> 

    <tx:annotation-driven transaction-manager="txManager" /> 

    <aop:config> 
    <aop:pointcut expression="execution(public * com.cdd.ssh.service.*.*(..))" 
        id="bussinessService"/> 
    <aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService" /> 
    </aop:config> 
    <tx:advice id="txAdvice" transaction-manager="txManager" > 
    <tx:attributes> 
        <tx:method name="query*"  read-only="true" propagation="NOT_SUPPORTED"  /> 
        <!-- get开头的方法不需要在事务中运行 。 
        有些情况是没有必要使用事务的,比如获取数据。开启事务本身对性能是有一定的影响的--> 
        <tx:method name="*"/>    <!-- 其他方法在实务中运行 --> 
    </tx:attributes> 
</tx:advice> 

</beans>

3个回答

qq_35973977
qq_35973977   2016.09.13 19:48

有人吗???求大神帮忙啊

wuxuyang_7788
wuxuyang_7788   2016.09.13 21:16

看看你的xml文件,你在update的时候是不是把对象的所有字段都update了,

qq_35973977
qq_35973977 大神配置文件也贴上去了,帮我看看吧,急死了,
大约一年之前 回复
qq_35973977
qq_35973977 用的注解方式,只有一个applicationContext.xml和struts.xml,没有hibernate的xml文件,全部整合到applicationContext.xml中了,
大约一年之前 回复
qq_35973977
qq_35973977   2016.09.14 09:29

找到了,应该是实体类中不能直接初始化set集合,在dao层每次都初始化新的set集合,问题解决了,但是还有一个问题,我自动生成的的中间表的两个关联属性都被自动勾上了主键属性,那我岂不是不能出现多对多关联了吗?这样只能一对一关联了,怎么办呢

Csdn user default icon
上传中...
上传图片
插入图片