简体   繁体   English

Spring Hibernate:不插入数据库的值

[英]Spring Hibernate: Values not inserting into database

I can't seem to add values to my table. 我似乎无法为我的表添加值。 There is no error but the values aren't inserting in my database. 没有错误,但值没有插入我的数据库中。 After submitting the form, no errors would show up. 提交表单后,不会出现任何错误。

Upon looking at the console, this shows: 查看控制台后,显示:

Hibernate: 
    insert 
    into
        referral
        (address, doctor_contact_no, doctor_name, facility_contact_no, facility_type, referral_no, referring_from, referring_to) 
    values
        (?, ?, ?, ?, ?, ?, ?, ?)

I already tried logging, just in case my controller can't read the form i'm submitting. 我已经尝试过记录,以防我的控制器无法读取我提交的表单。

Code Snippet of the Entity/Model i am saving 我正在保存的实体/模型的代码片段

@Entity
@Table(name="referral")
public class Referrals {
    @Id
    @Column
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    @Column
    private String referral_no;
    @Column
    private String facility_contact_no;
    @Column
    private String referring_from;
    @Column
    private String referring_to;
    @Column
    private String facility_type;
    @Column
    private String address;
    @Column
    private String doctor_name;
    @Column
    private String doctor_contact_no;' 

Code Snippet of my Services Class 我的服务类的代码片段

@Service
@Transactional
public class ReferralServicesImpl implements ReferralServices{
    @Autowired
    private ReferralDao referralDao;

    public List<Referrals> list(){
        return referralDao.list();
    }
    public boolean saveReferral(Referrals referral){

        if(referralDao.saveReferral(referral))
            return true;
        else
            return false;
    }
}

Code Snippet of the Controller Methods 控制器方法的代码片段

@RequestMapping(value = "/teleaudiology/referral", method = RequestMethod.GET)
        public ModelAndView showForm() {
            return new ModelAndView("referrals", "referrals", new Referrals());
        }

        @RequestMapping(value = "/teleaudiology/referrals", method = RequestMethod.POST)
        public String submit(@Valid @ModelAttribute("referrals")Referrals referral, 
          BindingResult result, ModelMap model) {
            if (result.hasErrors()) {
                return "error";
            }
            System.out.println("referral: "+referral.getDoctor_name());
            if(referralServices.saveReferral(referral))
                return "redirect:../teleaudiology";
            else 
                return "redirect:../teleaudiology";
        }

Here is the ReferralDao Class 这是ReferralDao类

public interface ReferralDao {
    public boolean saveReferral(Referrals referral);
    public List<Referrals> list();
}

ReferralDao impl ReferralDao impl

@Repository
@Transactional
public class ReferralDaoImpl implements ReferralDao {
    @Autowired
    SessionFactory session;
    Transaction trans;

    public boolean saveReferral(Referrals referral) {
        // TODO Auto-generated method stub
        //System.out.println("Saving..."+referral.getReferring_to_address());

        trans=session.getCurrentSession().beginTransaction();
        session.getCurrentSession().saveOrUpdate(referral);
        return true;
    }

    public List<Referrals> list() {
        // TODO Auto-generated method stub
        return session.getCurrentSession().createQuery("from Referrals").list();
    }

}

i tried using 我试过用

trans=session.getCurrentSession().getTransaction();

but resulted to this error 但导致了这个错误

`saveOrUpdate is not valid without active transaction`

Snippet from servlet-context.xml 来自servlet-context.xml的代码片段

<tx:annotation-driven transaction-manager="transactionManager" />
    <beans:bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="sessionFactory" />
    </beans:bean>

try using below code 尝试使用下面的代码

 Session s = sessionFactory.getCurrentSession();
 s.save(object);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM