-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: updatable = false except on delete?
PostPosted: Fri Jan 28, 2011 12:16 pm 
Newbie

Joined: Sun May 09, 2010 4:48 am
Posts: 11
An author of a project isn't changed as long as the column/project exists (updatable = false), it looks like this:
Code:
class Project {
    ...
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "author", updatable = false)
    public Student getAuthor() {             //this value can only be inserted on creation
        return author;
    }
   ...
}


What I tried looked like this, but this doesn't work:
Code:
class StudentDAO {
    ...
    public Student delete(Serializable id) {
        if (id instanceof Student) {
            id = ((Student) id).getId();
        }
        Student student = null;
        Session session = null;
        try {
            session = HibernateUtil.currentSession();
            session.beginTransaction();
            student = (Student) session.get(clazz, id);
            for (Project project : student.getProjects()) {
                project.setAuthor(null);                 //need to null out the reference here to delete the student without deleting the project
                student.getProjects().remove(project);
            }
            session.delete(student);
            session.getTransaction().commit();
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            throw e;
        }
        return student;
    }
    ...
}


So my current workaround is this, but is there a better/easier way:
Code:
class StudentDAO {
    ...
    public Student delete(Serializable id) {
        if (id instanceof Student) {
            id = ((Student) id).getId();
        }
        Student student = null;
        Session session = null;
        try {
            session = HibernateUtil.currentSession();
            session.beginTransaction();
            student = (Student) session.get(clazz, id);
            for (Project project : student.getProjects()) {
                final Integer pid = projekt.getId();
                session.doWork(new Work() {

                    @Override
                    public void execute(Connection connection)
                            throws SQLException {
                        connection.createStatement().execute(            //the current workaround I don't really like
                                "update project set "
                                + "author=null "
                                + "where id =" + pid);
                    }
                });
            }
            student.getProjects().clear();
            session.delete(student);
            session.getTransaction().commit();
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            throw e;
        }
        return student;
    }
    ...
}

Is it somehow possible use pure Hibernate code to do the same?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.