使用生成的ID时,数据不会保留

时间:2014-03-28 17:09:42

标签: eclipse spring hibernate jpa

我正在使用Spring Hibernate jsf和Jpa开发一个Web应用程序项目。 我实现了通用CRUD并使用生成的ID作为实体(@GeneratedValue(strategy = generationtype.identity)。第一个实体工作正常我可以保存并保存数据但是另一个事务提交正常但保存新行时进入我的数据库是添加Null值;你能解释一下这个的主要原因!

这是persistence.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
     <persistence  xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

      <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">

         <class>com.exemple.jeeapp.domain.utilisEntity</class>
           <class>com.exemple.jeeapp.domain.Conge</class>
          </persistence-unit>
             </persistence>

实体类

@Entity
@Table(name="conge")
public class Conge  implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 445434493929650665L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String nom;
private String prenom;
private String nature;
private Date debut;
private Date fin;

public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public String getNom() {
    return nom;
}
public void setNom(String nom) {
    this.nom = nom;
}

public String getPrenom() {
    return prenom;
}
public void setPrenom(String prenom) {
    this.prenom = prenom;
}

public String getNature() {
    return nature;
}
public void setNature(String nature) {
    this.nature = nature;
}

public Date getFin() {
    return fin;
}
public void setFin(Date fin) {
    this.fin = fin;
}

public Date getDebut() {
    return debut;
}
public void setDebut(Date debut) {
    this.debut = debut;
}


   }

和实体ServiceImpl类

    public class CongeServiceImpl implements CongeService{

@Autowired
private CongeDao congeDao;

public boolean createConge(Conge conge) {

    try{
        congeDao.save(conge);
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Congé ajoutée"));
    }catch(Exception e){
        e.printStackTrace();
        return false;
    }

    return true;

}


public ArrayList<Conge> getConges() {

    return (ArrayList<Conge>) congeDao.findAll();
}


public CongeDao getCongeDao() {
    return congeDao;
}


public void setCongeDao(CongeDao congeDao) {
    this.congeDao = congeDao;
}

  }

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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

   <import resource="webflow-config.xml"/>
   <import resource="datasource-config.xml"/>
   <import resource="security-config.xml"/>

   <!-- DAO declarations -->

   <bean id="UserDao" class="com.exemple.jeeapp.dao.UserJpaDao"/>

   <bean id="CongeDao" class="com.exemple.jeeapp.dao.CongeDaoImpl"/>


   <!-- Services declarations -->

   <bean id="userService" class="com.exemple.jeeapp.services.impl.UserServiceImpl">
   <property name="userDao" ref="UserDao"></property>
   </bean>



   <bean id="congeService" class="com.exemple.jeeapp.services.impl.CongeServiceImpl">
   <property name="congeDao" ref="CongeDao"></property>
   </bean>

   <bean id="userAuthenticationProviderService"       class="com.exemple.jeeapp.services.impl.UserAuthenticationProviderServiceImpl">
   <property name="authenticationManager" ref="authenticationManager"/>
   </bean>


 </beans>

0 个答案:

没有答案