在试图获得@OneToMany
文章和标题字段
之间的关系时,我可能没有完全正确的映射,导致:
init:
Deleting: /home/thufir/NetBeansProjects/USENET/build/built-jar.properties
deps-jar:
Updating property file: /home/thufir/NetBeansProjects/USENET/build/built-jar.properties
compile:
run:
DEBUG: nntp: newsrc loading /home/thufir/.newsrc
DEBUG: nntp: newsrc load: 1 groups in 31ms
[EL Info]: 2012-07-31 02:05:05.677--ServerSession(8979162)--EclipseLink, version: Eclipse Persistence Services - 2.3.0.v20110604-r9504
[EL Info]: 2012-07-31 02:05:06.778--ServerSession(8979162)--file:/home/thufir/NetBeansProjects/USENET/build/classes/_USENETPU login successful
[EL Warning]: 2012-07-31 02:05:06.903--ServerSession(8979162)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'usenet.HEADERFIELD' doesn't exist
Error Code: 1146
Call: ALTER TABLE HEADERFIELD DROP FOREIGN KEY FK_HEADERFIELD_ARTICLE_ID
Query: DataModifyQuery(sql="ALTER TABLE HEADERFIELD DROP FOREIGN KEY FK_HEADERFIELD_ARTICLE_ID")
[EL Warning]: 2012-07-31 02:05:06.916--ServerSession(8979162)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'ARTICLE'
Error Code: 1051
Call: DROP TABLE ARTICLE
Query: DataModifyQuery(sql="DROP TABLE ARTICLE")
[EL Warning]: 2012-07-31 02:05:07.033--ServerSession(8979162)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'NEWSGROUP'
Error Code: 1051
Call: DROP TABLE NEWSGROUP
Query: DataModifyQuery(sql="DROP TABLE NEWSGROUP")
[EL Warning]: 2012-07-31 02:05:07.122--ServerSession(8979162)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'HEADERFIELD'
Error Code: 1051
Call: DROP TABLE HEADERFIELD
Query: DataModifyQuery(sql="DROP TABLE HEADERFIELD")
[EL Warning]: 2012-07-31 02:05:08.921--UnitOfWork(26970615)--java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: null.
Jul 31, 2012 2:05:08 AM net.bounceme.dur.usenet.driver.Main main
SEVERE: null
javax.persistence.RollbackException: java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: null.
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:102)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:63)
at net.bounceme.dur.usenet.driver.Main.persistArticle(Main.java:67)
at net.bounceme.dur.usenet.driver.Main.<init>(Main.java:43)
at net.bounceme.dur.usenet.driver.Main.main(Main.java:24)
Caused by: java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: null.
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.discoverUnregisteredNewObjects(RepeatableWriteUnitOfWork.java:302)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.calculateChanges(UnitOfWorkImpl.java:695)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1482)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitRootUnitOfWork(RepeatableWriteUnitOfWork.java:265)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1135)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:84)
... 4 more
[EL Info]: 2012-07-31 02:05:09.332--ServerSession(8979162)--file:/home/thufir/NetBeansProjects/USENET/build/classes/_USENETPU logout successful
BUILD SUCCESSFUL (total time: 9 seconds)
文章:
package net.bounceme.dur.usenet.model;
import java.io.Serializable;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.persistence.*;
@Entity
public class Article implements Serializable {
private static final long serialVersionUID = 1L;
private static final Logger LOG = Logger.getLogger(Article.class.getName());
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String subject;
@OneToMany(mappedBy = "article", cascade = CascadeType.PERSIST)
private List<HeaderField> headerFields = new ArrayList<>();
public Article() {
}
public Article(Message message) {
try {
subject = message.getSubject();
Enumeration e = message.getAllHeaders();
while (e.hasMoreElements()) {
Header header = (Header) e.nextElement();
@SuppressWarnings("unchecked")
SimpleEntry nameValue = new SimpleEntry(header.getName(), header.getValue());
HeaderField headerField = new HeaderField(nameValue);
headerFields.add(headerField);
}
} catch (MessagingException ex) {
Logger.getLogger(Article.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Article)) {
return false;
}
Article other = (Article) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return subject;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}
海德菲尔德:
package net.bounceme.dur.usenet.model;
import java.io.Serializable;
import java.util.AbstractMap.SimpleEntry;
import java.util.logging.Logger;
import javax.persistence.*;
@Entity
public class HeaderField implements Serializable {
private static final long serialVersionUID = 1L;
private static final Logger LOG = Logger.getLogger(HeaderField.class.getName());
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
private Article article = new Article();
@Column
private String headerName;
@Column
private String headerValue;
public HeaderField() {
}
public HeaderField(SimpleEntry nameValue) {
headerName = nameValue.getKey().toString();
headerValue = nameValue.getValue().toString();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof HeaderField)) {
return false;
}
HeaderField other = (HeaderField) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "\n\nheaderName\t" + headerName + "\nheaderValue\t" + headerValue;
}
public String getHeaderName() {
return headerName;
}
public void setHeaderName(String headerName) {
this.headerName = headerName;
}
public String getHeaderValue() {
return headerValue;
}
public void setHeaderValue(String headerValue) {
this.headerValue = headerValue;
}
}
实体已经更新,以反映使用CascateType. PERSIST
的建议,这似乎可以修复特定的错误消息。我正在努力生成一个更有用的日志来跟踪发生的事情。
我为我工作并删除了cascade属性:
OneToMany(mappedBy = "article")
private List<HeaderField> someOrAllHeaderFields = new ArrayList<>();
只需添加CascateType。所有关于您的关系
OneToMany(mappedBy = "article", cascade = CascadeType.ALL)
private List<HeaderField> someOrAllHeaderFields = new ArrayList<>();
您可能已经创建了文章和HeaderField的新实例。HeaderField的这些实例随后与文章关联。
之后,尝试持久化文章失败了,因为正如错误消息所说,它引用了新对象,并且关系没有标记为持久化。此外,根据您的日志,这些HeaderField实例没有设置headerName和headerValue。
你有两个选择:
>
从文章到头字段的级联持久化操作如下
OneToMany(mappedBy = "article", cascade = CascadeType.PERSIST)
private List<HeaderField> someOrAllHeaderFields = new ArrayList<>();
此外,不应删除任何参数构造函数<代码>JPA实现在创建实例时总是调用此构造函数。
但是你不能让arg构造函数受到保护。在JPA 2.0规范中,这是通过以下文字来说明的:
实体类必须有一个no-arg构造函数。实体类也可以有其他构造函数。no-arg构造函数必须是公共的或受保护的。
这是我的第一篇帖子,如果我有任何错误,请原谅我。 当我想查询(添加,编辑,删除)数据库时,我得到一个错误java.lang.IllegalStateExctive:在同步期间,通过未标记为级联PERSIST的关系找到了一个新对象。 相关表格包括赞助商和捐款。它们之间存在一对多的关系。以下课程:赞助商 捐款:
在同步过程中,我发现了一个错误
我有两个2类的关系多对多。 我想用这种方式创建一个新的recipie: 如果数据库中不存在成分,则会出现如下错误 有没有办法自动添加表中创建的对象? 我尝试添加CascateType. PERSIST但它也不工作
我无法使用OSMbonuspack标记,因为无法识别导入 给出的错误不能解析符号“marker”,尽管项目中有osmdroid(v5.6.4)和osmbonuspack(v6.3)。 有没有人知道这为什么不行?
我最近遇到了一个问题。 我需要在页面上找到一个包含特定文本的div标签。问题是,文本被内部链接标记分成两部分,因此超文本标记语言树看起来像: 为了唯一地标识div标记,我需要div文本的两部分。当然,我会想出类似XPath的方法: 但是,它不起作用,第二部分找不到。 什么是唯一描述这种标签的最佳方法?
考虑到Laravel指南中的基本示例有许多通过关系,我试图直接查询雄辩的、帖子类型为100的国家(只是一个例子)。 type_id是我的特例... 因此,我在country模型中创建了一个关系,通过用户创建了与帖子的关系。我想要实现的结果是列出一个国家,以及每个国家内由该国用户创建的帖子。 我能够以其他方式复制我想要实现的结果,但是我想知道为什么下面雄辩的查询没有返回预期的结果: 查询的返回是几个