-->
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.  [ 3 posts ] 
Author Message
 Post subject: Join Column with lookup table
PostPosted: Fri Jul 01, 2016 6:41 pm 
Newbie

Joined: Fri Jul 01, 2016 6:16 pm
Posts: 2
This is the table structure i have right now. The look up table is changed rarely or may even not be changed.
User Color
id id
name name
color_id

There is MANY TO ONE uni directional relationship between User and Color table.
When i do insert or update, I just want to insert it into user table only and use the color table just to get the color_id.

Below is the rough structure of code that i have written but i keep getting multiple errors.
USER
Code:
@Entity
@Table(name = "user")
public class User {

   @Id @GeneratedValue(strategy=GenerationType.AUTO)
   private long id;
   
   @Column(name = "name")
   private String name;

        @ManyToOne
   @JoinColumn(name="color_id")
   private Color color;
   


COLOR
Code:
public class Color {

   @Id @GeneratedValue(strategy=GenerationType.AUTO)
   private long id;
   
   @Column(name = "name")
   private String name;



When i use the above code it works but The color class is also saved in the database.
ERROR 1
If the use
Code:
        @ManyToOne(fetch = FetchType.EAGER, optional = false)
   @JoinColumn(name="color_id", nullable=false, insertable=false, updateable=false)
   private Color color;


The value of color is NULL, and also the color table is saved.

ERROR 2
If i only try to save the user object but not the color obj, by getting the color id from another query i keep getting
Quote:
transient instance must be saved before current operation : persistence.hibernate.User.color -> persistence.hibernate.Color


Can anyone who have been in this similar situation help me please.


Top
 Profile  
 
 Post subject: Re: Join Column with lookup table
PostPosted: Sat Jul 02, 2016 2:05 am 
Newbie

Joined: Sun May 01, 2016 11:34 am
Posts: 2
public class Color {

@Id @GeneratedValue(strategy=GenerationType.AUTO)
private long id;

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

@OneToMany(fetch = FetchType.LAZY, mappedBy = "color")
private List<User> user;


// of course (class User)
private void setColor(Color color){
this.color = color;
}


Top
 Profile  
 
 Post subject: Re: Join Column with lookup table
PostPosted: Sat Jul 02, 2016 3:21 am 
Newbie

Joined: Fri Jul 01, 2016 6:16 pm
Posts: 2
Thanks, Newbie. It works now.


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

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.