splatter_fadli
splatter_fadli

Reputation: 781

how to avoid updating parent in JPA Hibernate One-to-Many

I made a project that using spring + hibernate (JPA Mapping). I'm using One-to-Many JPA Hibernate. i don't know whether my design itself wrong or what. I have two class Useradmin (Parent) and Tanya (child). from this i need to get the records from another table by using properties of Useradmin (parent). when I insert new record for Tanya (child) the Useradmin (Parent) updated too. I just want to insert into Tanya (child) whitout insert or update the Useradmin (Parent). could any body give me a solution, please suggest the idea to proceed :)

this is my Useradmin Class :

@SuppressWarnings("serial")
@Entity
@Table(name="user")
public class UserAdmin implements Serializable{

@Id
@Column(name="no_dana", unique=true, nullable=false, updatable=false)
private String no_dana;

@Column(name="npp")
private String npp;

@Column(name="password")
private String password;

@Column(name="nama_depan")
private String nama_depan;

@Column(name="nama_tengah")
private String nama_tengah;

@Column(name="nama_belakang")
private String nama_belakang;

@Column(name="kota")
private String kota;

@OneToMany(cascade=CascadeType.ALL, mappedBy="useradmin")
private Set<Tanya> tanya  = new HashSet<Tanya>(0);

/**other field**//

/**setter and gettter methods**/
}

this is my Tanya Class :

@SuppressWarnings("serial")
@Entity
@Table(name="tanya")
public class Tanya implements Serializable{

@Id
@Column(name="id_tanya", unique=true, nullable=false)
private int id_tanya;

@Column(name="subject")
private String subject;

@Column(name="isi")
private String isi;

@Temporal(TemporalType.TIMESTAMP)
@Column(name="created")
private Date created;

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="no_dana")
private UserAdmin useradmin;

/**other field**//

/**setter and gettter methods**/
}

and this is my controller :

@RequestMapping("/user/tanya-jawab.html")
public ModelAndView listTanyaJawab(ModelMap model)throws Exception
{
    User user           = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    String sessionUser  = user.getUsername();

    try{
        UserAdmin dataUser = userService.get(sessionUser);
        model.addAttribute("userData", dataUser);
    } catch(Exception e){
        e.printStackTrace();
    }

    ModelAndView mav = new ModelAndView("usr-tanya");
    List<Tanya> tanyajawab = tanyaService.listAllTanya();
    mav.addObject("tanyajawab", tanyajawab);
    return mav;
}

@RequestMapping(value="/user/add-tanya-jawab.html", method=RequestMethod.POST)
public ModelAndView addTanyaJawab(@ModelAttribute Tanya tanya, ModelMap model, HttpServletRequest request, HttpServletResponse response)throws Exception
{
    String userName = request.getParameter("hiddenUsername");

    UserAdmin useradmin = new UserAdmin();
    useradmin.setNo_dana(userName);
    System.out.println("ini"+userName);     
    java.util.Date date = new java.util.Date();

    tanya.setUseradmin(useradmin);
    tanya.setCreated(date);
    tanyaService.save(tanya);       

    return listTanyaJawab(model);
}

any help will be pleasure :)

Upvotes: 2

Views: 3153

Answers (2)

ujulu
ujulu

Reputation: 3309

There are couple of things you can modify (even though it is not a good idea except in a test environment):

  • As already mentioned in comment area remove the cascade attribute from the @ManyToOne annotation.
  • Don't set useradmin into tanya object as in:

    tanya.setUseradmin(useradmin);
    

before tanya object is saved to the database. Otherwise, the persistence provider will throw IllegalStateException because the cascade attribute is removed. This is logical because your Tanya entity contains a foreign key to the UserAdmin entity the provider has to check if there is an entry for the useradmin in the database before it saves tanya.

But you created a new instance of UserAdmin as follows:

UserAdmin useradmin = new UserAdmin();

and therefore, there is no entry in the database. So the provider cannot save tanya which is pointing to a non-existent entry of useradmin into DB.

And some advices to improve your code:

  • Remove unnecessary @Column annotation as long as you do not modify the default, for example in

    @Column(name="isi")
    private String isi;
    

    it is useless because it is the default behavior.

  • Why @SupressWarnings annotation? This is unnecessary IMHO.

  • And lastly, the following in a real environment is very dangerous:

    cascade = CascadeType.ALL
    

    Especially the CascadeType.REMOVE.

Upvotes: 0

Markus
Markus

Reputation: 1016

If I understand this correctly the issue seems to occur in your

public ModelAndView addTanyaJawab(@ModelAttribute Tanya tanya, ModelMap model, HttpServletRequest request, HttpServletResponse response)throws Exception {...}

method. You are creating a new Useradmin object there with:

    UserAdmin useradmin = new UserAdmin();

The issue with that is that this object is new and has not been persisted into the database yet. When you are creating a new Tanya object you are referencing it to the new UserAdmin object. However this doesn't exist.

You are either required to set a persistent UserAdmin object in your Tanya object wich has been loaded from the database beforehand or create a new UserAdmin object, add Tanya and save that instead.

Upvotes: 2

Related Questions