简体   繁体   中英

Hibernate 4 Save not working

I'm new to hibernate 4 and I'm having some problem while saving a object using save(Object) method. My mapping files generated using reverse engineering are give below.

Event.java

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

private Integer eventId;
private Set<EventCategory> eventCategories = new HashSet<EventCategory>(0);

public Event() {
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "EVENT_ID", unique = true, nullable = false)
public Integer getEventId() {
    return this.eventId;
}

public void setEventId(Integer eventId) {
    this.eventId = eventId;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "event")
public Set<EventCategory> getEventCategories() {
    return this.eventCategories;
}
public void setEventCategories(Set<EventCategory> eventCategories) {
    this.eventCategories = eventCategories;
}

}

category.java

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

private Integer categoryId;
private Set<EventCategory> eventCategories = new HashSet<EventCategory>(0);

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "CATEGORY_ID", unique = true, nullable = false)
public Integer getCategoryId() {
    return this.categoryId;
}

public void setCategoryId(Integer categoryId) {
    this.categoryId = categoryId;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "category")
public Set<EventCategory> getEventCategories() {
    return this.eventCategories;
}

public void setEventCategories(Set<EventCategory> eventCategories) {
    this.eventCategories = eventCategories;
}

}

EventCategory.java

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

private EventCategoryId id;
private Event event;
private Category category;

public EventCategory() {
}

public EventCategory(EventCategoryId id) {
    this.id = id;
}

public EventCategory(EventCategoryId id, Event event, Category category) {
    this.id = id;
    this.event = event;
    this.category = category;
}

@EmbeddedId
@AttributeOverrides({
        @AttributeOverride(name = "eventId", column = @Column(name = "EVENT_ID")),
        @AttributeOverride(name = "categoryId", column = @Column(name = "CATEGORY_ID")) })
public EventCategoryId getId() {
    return this.id;
}

public void setId(EventCategoryId id) {
    this.id = id;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "EVENT_ID", insertable = false, updatable = false)
public Event getEvent() {
    return this.event;
}

public void setEvent(Event event) {
    this.event = event;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "CATEGORY_ID", insertable = false, updatable = false)
public Category getCategory() {
    return this.category;
}

public void setCategory(Category category) {
    this.category = category;
}

}

All these files are generated using hbm2java (reverse engineering). But i couldn't able to save values using the below code. And also I'm not getting any exception while running this piece of code.

EventCategory eventCategory = new EventCategory();
eventCategory.setEvent(event);
eventCategory.setCategory(category);
eventCategory.setId(eventCategoryId);
getSession().save( eventCategory );

Please help me to resolve my problem

You need a transaction.

EventCategory eventCategory = new EventCategory();
eventCategory.setEvent(event);
eventCategory.setCategory(category);
eventCategory.setId(eventCategoryId);

Session session = getSession();
Transaction transaction = session.beginTransaction();
session.save( eventCategory );
transaction.commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM