Java 调用刷新方法时EmbeddedId出错(Spring+;JPA 2 hibernate)
调用刷新方法时出错。我有以下表格:本体、发布和本体逐发布 这是本体的pk:ontology|u id|auto increment 这是出版物的主键:出版物| id |自动递增 这是本体论按发布pk:发布发布id、本体论本体id、本体论按发布id 列本体按发布id设置了自动增量 我的本体论发布如下所示:Java 调用刷新方法时EmbeddedId出错(Spring+;JPA 2 hibernate),java,spring,hibernate,jpa,Java,Spring,Hibernate,Jpa,调用刷新方法时出错。我有以下表格:本体、发布和本体逐发布 这是本体的pk:ontology|u id|auto increment 这是出版物的主键:出版物| id |自动递增 这是本体论按发布pk:发布发布id、本体论本体id、本体论按发布id 列本体按发布id设置了自动增量 我的本体论发布如下所示: @Entity @Table(name = "ontology_by_publication") public class OntologyByPublication implements Se
@Entity
@Table(name = "ontology_by_publication")
public class OntologyByPublication implements Serializable {
/**
*
*/
private static final long serialVersionUID = -7427863218422284148L;
@EmbeddedId
private OntologyByPublicationId ontologyByPublicationId;
@Column(name = "description")
private String description;
@Column(name = "current_price")
private Double currentPrice;
@MapsId(value = "ontologyId")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ontology_ontology_id")
private Ontology ontology;
@MapsId(value = "publicationId")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "publication_publication_id")
private Publication publication;
getters - setters
这是@Embeddeble类:
@Embeddable
public class OntologyByPublicationId implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Column(name = "ontology_by_publication_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long ontologyByPubId;
@Column(name = "publication_publication_id")
private Long publicationId;
@Column(name = "ontology_ontology_id")
private Integer ontologyId;
我调用以下方法来持久化OntologyByPublication对象:
public OntologyByPublication saveOntologyByPublicationRefresh(
OntologyByPublication ontologyByPublication) {
try
{
em.persist(ontologyByPublication);
em.flush();
em.refresh(ontologyByPublication);
return ontologyByPublication;
}
catch (Exception ex)
{
return null;
}
}
执行包含em.refresh的行时,将引发此异常:
javax.persistence.EntityNotFoundException: No row with the given identifier exists: [org.springtest.mavenspringapp.domain.OntologyByPublication#org.springtest.mavenspringapp.domain.OntologyByPublicationId@73ba7b18]
我不知道我做错了什么。请帮助我。如果您在控制台上启用sql,您将看到在执行持久化操作后,hibernate是否能够将对象保存到数据库中。同时检查数据库是否同时保存。我希望您执行em.merge(object)而不是persist,这是您试图保存的新对象吗?@Zeus是的,这是一个新对象。。我正在使用服务器上的调试来查看发生了什么。所以,在调试时,您是否看到正在保存的对象?像hibernate日志上的insert语句?另外,您看到保存在DB中的行了吗?是的,我可以看到insert语句,在调用refresh方法之前,一切都很顺利。发生这种情况时,hibernate显示它试图执行select语句,但出现异常,异常EntityNotFound。即使发生了插入,当我检查数据库时,表中也没有记录。