开发者

Inserting a Hibernate entity with a many-to-one relationship

开发者 https://www.devze.com 2023-02-19 13:50 出处:网络
I\'m fairly new to Hibernate and have a question on how to handle an Insert when the entity has a FK; specifically how to create the entity to be inserted.

I'm fairly new to Hibernate and have a question on how to handle an Insert when the entity has a FK; specifically how to create the entity to be inserted.

My entities have the following structure simplified:

@Entity
@Table(name="event")
public class Event implements java.io.Serializable {

    private int id;
    private int eventType
    private User user;  
    
    public Event (int id, int eventType, User user) {
        this.id = id;
        this.eventType = eventType;
        this.user = user;
    }

    @Id
    @Column(name="ID")
    public int getId() {
        return this.id;
    }

    @Column(name="EVENT_TYPE", nullable=false)
    public int getEventType() {
        return this.eventType;
    }
    
    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, targetEntity=User.class )
    @JoinColumn(name="USER_ID")
    public User getUser() {
        return this.user;
    }
    ...
}

-----------

@Entity
@Table(name="user")
public class User implements java.io.Serializable {

    private int id;
    private String name;
        
    public User (int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Id
    @Column(name="ID")
    public int getId() {
        return this.id;
    }

    @Column(name="Name", nullable=false)
    public String getName() {
        return this.name;
    }
    ...
}

Now I want to persist a new Event, and I have the UserID asociated to the User of that event.

I need to create something like:

Event event开发者_JAVA百科ToInsert = new Event(45,6,  *User object I don't have*);

to later tell hibernate to save eventToInsert.

My problem is I only have the UserID, but to create the Event, I need the entire User object and not only its ID.

So, the questions:

-Is this a design fault? Should Event only have an 'int userID' field instead of 'User user'?

-Does Hibernate have any way to dealing with this problem? Something like:

Event eventToInsert = new Event(45,6, Hibernate.getEntity(UserTable, UserIdValue);

-Which would be the appropiate way of doing this?

Thanks for any help.


You should be able to put a one to many on the User, something like:

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private final Set<Event> events = new HashSet<Event>();

then you would just need to get the user by the Id you have, add the event to the collection, and persist. I believe that should work.


Event eventToInsert = new Event(45, 6, (User) session.load(User.class, userIdValue));

But if you don't know about Session.load and Session.get, I suggest you read the Hibernate documentation. This is the most basic stuff : loading an entity by its primary key.

Also, note that having a cascade on a ManyToOne is bizarre : it means that you'll potentially create/modify the user each time you modify one of its events.

0

精彩评论

暂无评论...
验证码 换一张
取 消