org.hibernate.exception.sqlgrammareexception:无法初始化集合
我有三个表tbl_category、tbl_Advertisiment和tbl_linkcategoryadvert(有多对多关联b/w category&Advertisiment) 我的POJO如下所示 Advertisment.javaorg.hibernate.exception.sqlgrammareexception:无法初始化集合,hibernate,jpa,Hibernate,Jpa,我有三个表tbl_category、tbl_Advertisiment和tbl_linkcategoryadvert(有多对多关联b/w category&Advertisiment) 我的POJO如下所示 Advertisment.java @Entity @Table(name="tbl_advertisment") public class Advertisment implements Serializable { private long id; private Str
@Entity
@Table(name="tbl_advertisment")
public class Advertisment implements Serializable {
private long id;
private String title;
private String message;
private Set<Category> categories;
public Advertisment(String title, String message) {
this.title = title;
this.message = message;
this.categories=new HashSet<Category>();
}
protected Advertisment() {
}
@Id
@GeneratedValue
protected long getId() {
return id;
}
protected void setId(long id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Column(nullable=false)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@ManyToMany(mappedBy="adverts")
public Set<Category> getCategories() {
return categories;
}
public void setCategories(Set<Category> categories) {
this.categories = categories;
}
}
@Entity
@Table(name="tbl_category")
public class Category implements Serializable {
private long id;
private String title;
private Set<Advertisment> adverts=new HashSet<Advertisment>();
public Category(String title) {
this.title = title;
}
protected Category() {
}
@ManyToMany
@JoinTable(name="tbl_linkcategoryadvert")
public Set<Advertisment> getAdverts() {
return adverts;
}
public void setAdverts(Set<Advertisment> adverts) {
this.adverts = adverts;
}
public void addAdvert(Advertisment advert){
adverts.add(advert);
}
@Id
@GeneratedValue
protected long getId() {
return id;
}
protected void setId(long id) {
this.id = id;
}
@Column(unique=true,nullable=false)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
DAO.java
@Entity
@Table(name="tbl_advertisment")
public class Advertisment implements Serializable {
private long id;
private String title;
private String message;
private Set<Category> categories;
public Advertisment(String title, String message) {
this.title = title;
this.message = message;
this.categories=new HashSet<Category>();
}
protected Advertisment() {
}
@Id
@GeneratedValue
protected long getId() {
return id;
}
protected void setId(long id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Column(nullable=false)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@ManyToMany(mappedBy="adverts")
public Set<Category> getCategories() {
return categories;
}
public void setCategories(Set<Category> categories) {
this.categories = categories;
}
}
@Entity
@Table(name="tbl_category")
public class Category implements Serializable {
private long id;
private String title;
private Set<Advertisment> adverts=new HashSet<Advertisment>();
public Category(String title) {
this.title = title;
}
protected Category() {
}
@ManyToMany
@JoinTable(name="tbl_linkcategoryadvert")
public Set<Advertisment> getAdverts() {
return adverts;
}
public void setAdverts(Set<Advertisment> adverts) {
this.adverts = adverts;
}
public void addAdvert(Advertisment advert){
adverts.add(advert);
}
@Id
@GeneratedValue
protected long getId() {
return id;
}
protected void setId(long id) {
this.id = id;
}
@Column(unique=true,nullable=false)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
我不提供DAO.java,因为它只包含Hibernate样板代码
当我运行main类时,会出现以下异常:
SEVERE: Unknown column 'adverts0_.categories_id' in 'field list'
org.hibernate.exception.SQLGrammarException: could not initialize a collection: [hibernate.Category.adverts#3]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2001)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
at org.hibernate.collection.PersistentSet.add(PersistentSet.java:189)
at hibernate.Category.addAdvert(Category.java:48)
at hibernate.client.PostAdvertisment.post(PostAdvertisment.java:35)
at hibernate.client.PostAdvertisment.main(PostAdvertisment.java:56)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'adverts0_.categories_id' in 'field list'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2113)
请帮帮我
我的表格如下所示: 1。tbl_广告
id PK BIGINT
title VARCHAR(255)
advert_id FK references(advert.id)
category_id FK references(category.id)
2。tbl_类别
id PK BIGINT
title VARCHAR(255)
3。tbl_linkcategoryadvert
id PK BIGINT
title VARCHAR(255)
advert_id FK references(advert.id)
category_id FK references(category.id)
现在我的问题是
您希望在拥有关系的类上使用@Jointable注释。您需要在广告类上放置@Jointable注释。Hibernate正在为您的join列生成列名“categories\u id”,其中您的表使用名称“category\u id”。参考指南中有一个示例。在周围的文本中也有一些相关的细节。您只需将Category.getAdverts()上的映射设置为:
@JoinTable(
name="tbl_linkcategoryadvert",
joinColumns=@JoinColumn(name="category_id"),
inverseJoinColumns=@JoinColumn(name="advert_id")
)
联接表tbl_linkcategoryadvert中的外键是否具有名称Category_id和Advertisiments_id?否则,您可能必须配置@JoinTable映射以反映这些名称。您能告诉我
广告
表是什么样子吗?也许您需要定义多对多列,例如@JoinTable(name=“adverts”,joinColumns=@JoinColumn(name=“cat\u id”)
?请以后编辑您的问题以提供更多详细信息。此外,请查看编辑器中的格式帮助链接。