【问题标题】:Hibernate 4 Save not working休眠4保存不起作用
【发布时间】:2014-06-02 19:47:12
【问题描述】:

我是 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 );

请帮我解决我的问题

【问题讨论】:

  • 那么,有什么问题呢?数据库中什么都没有出现?您是否将此代码包含在最后提交的事务中?
  • @JBNizet 感谢您的评论。是的,你是对的。它在使用下面帖子中给出的交易后工作。

标签: java hibernate


【解决方案1】:

你需要一笔交易。

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

【讨论】:

  • 你是对的。包含在 Transaction 中后,它工作正常。但我怀疑我是否能够在没有任何交易的情况下保存其他对象(如事件)。为什么我需要单独为这个对象使用事务。我使用以下代码保存事件对象并将其保存在数据库中。 int eventID = (Integer) getSession().save(event);
  • 其他代码是否在托管环境中运行? CMT/EJB?使用 CMT,事务在会话 bean 中自动处理(甚至回滚)。因此,在这种情况下不需要事务代码。
  • 你用的是spring吗?您的 Event 对象是否有您的 EventCategory 缺失的 @Transactional 注释?
  • 是的,我正在使用 spring,但我没有使用 EJB/CMT。并且 Event 类中也不存在@Transactional。
猜你喜欢
  • 2015-01-30
  • 1970-01-01
  • 1970-01-01
  • 2016-06-23
  • 1970-01-01
  • 2011-12-16
  • 2015-10-28
  • 2023-03-22
相关资源
最近更新 更多