Hibernate 4保存不起作用

时间:2014-06-02 19:47:13

标签: java hibernate

我是hibernate 4的新手,我在使用 save(Object)方法保存对象时遇到了一些问题。我使用逆向工程生成的映射文件如下所示。

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;
}

}

所有这些文件都是使用hbm2java(逆向工程)生成的。但是我无法使用以下代码保存值。而且我在运行这段代码时也没有遇到任何异常。

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

请帮我解决我的问题

1 个答案:

答案 0 :(得分:2)

您需要一笔交易。

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();