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 1If 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 2If 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.