Solved

Hibernate cascade deletes but does not save

Posted on 2014-10-06
1
144 Views
Last Modified: 2014-10-12
UserprojectEntity has a OnetoMany relation with LinereferenceEntity. My cascade works for delete but not save.

UserprojectEntity:

  @XmlElementWrapper(name = "linereferencelist")
  @OneToMany(fetch = FetchType.LAZY,  mappedBy = "upl")
    @Cascade(CascadeType.ALL)
    @OrderBy("id ASC")
    public Collection<LinereferenceEntity> getLinereference() {
        return linereference;
    }

    public void setLinereference(Collection<LinereferenceEntity> linreference) {
        this.linereference = linreference;
    }

LinereferenceEntity:

 private UserprojectEntity upl;
    @XmlTransient
    @ManyToOne
    @JoinColumns({@JoinColumn(name = "user", referencedColumnName = "user", nullable = false,insertable = false,updatable = false), @JoinColumn(name = "project", referencedColumnName = "project", nullable = false,insertable = false,updatable = false)})
    public UserprojectEntity getUpl() {
        return upl;
    }

    public void setUpl(UserprojectEntity upl) {
        this.upl = upl;
    }
}

Calling program:


    private void deleteRecords(String sConfig, String sDelstring){
        SessionFactory sessionFactory = commonFuncs.getSessionFactory(sConfig) ;

        Session session = sessionFactory.openSession();
        Transaction transaction;

        transaction = session.beginTransaction();

        Query q = session.createQuery(sDelstring);

        Object upe = q.list().get(0);
        session.delete(upe);
        transaction.commit();
        session.close();

    }


 private void saveRecords(String sConfig, Object obj){
        SessionFactory sessionFactory = commonFuncs.getSessionFactory(sConfig) ;

        Session session = sessionFactory.openSession();
        Transaction transaction;
        transaction = session.beginTransaction();


        session.save(obj);

        transaction.commit();
        session.close();

    }
0
Comment
Question by:fblack61
1 Comment
 

Accepted Solution

by:
fblack61 earned 0 total points
ID: 40375777
I had to use merge.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 4 159
servlet  URL Rewriting 1 25
diffSum example 4 21
PHP - AJAX and MySQL it works only if the value is a number 12 34
Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now