OneToMany relation via @JoinTable results in hibernate selecting invalid column
up vote
0
down vote
favorite
Simplified example of what's happening
Entity 1
@Entity
@Table("config")
public class Configuration implements serializable {
private Long id;
private List<ColumnGroup> columnGroups;
@Id
@GeneratedValue
@Column(name="ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@JoinTable(
name="config_column_group",
joinColumns=@JoinColumn(name="config_id", referencedColumnName="id"),
inverseJoinColumns=@JoinColumn(name="column_group_id", referencedColumnName="id"
)
List<ColumnGroup> getColumnGroups() {
return columnGroups;
}
public void setColumnGroups(List<ColumnGroup> columnGroups) {
this.columnGroups = columnGroups;
}
}
Entity 2:
@Entity
@Table("column_group")
public class ColumnGroup implements serializable {
private Long id;
private List<Column> columns;
@Id
@GeneratedValue
@Column("ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@OneToMany(cascade=CascadeType.ALL, mappedBy="columnGroup", fetch=FetchType.LAZY)
public List<Column> getColumns() {
return columns;
}
public void setColumns(List<Column> columns) {
this.columns = columns;
}
}
Then, I get an instance of the entity and call getColumnGroups():
Config config = (Config)HibernateManager.getSession().get(Config.class, id);
List<ColumnGroup> = config.getColumnGroups();
And I get the exception
org.hibernate.exception.SQLGrammarException: could not initialize a collection: [Configuration.columnGroups]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2173)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:627)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1863)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:369)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
...
Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "COLUMNGR1_"."CONFIG_ID": invalid identifier
And looking at the sql being run by hibernate, I see
select
columngrou0_.config_id as vt1_107_1_,
columngrou0_.column_group_id as column2_1_,
vtcolumngr1_.ID as ID109_0_,
vtcolumngr1_.CONFIG_ID as VT9_109_0_
from
config_column_group columngrou0_
inner join
COLUMN_GROUP vtcolumngr1_
on columngrou0_.column_group_id=columngr1_.ID
where
columngrou0_.vt_config_id=?
So it looks as if hibernate is trying to select a column that exists in the join table from the owning table, and it isn't selecting the target entity at all.
That would suggest to me that I didn't put the column names in the right place in the JoinTable/JoinColumn annotation, but I am following an example that works, and even if I do change things around, there doesn't seem to be any winning combination...
The tables are structured as you might expect-- config_column_group has id (pk), config_id (fk referencing config(id)), and column_id (fk referencing column_group(id)).
java hibernate one-to-many jointable
add a comment |
up vote
0
down vote
favorite
Simplified example of what's happening
Entity 1
@Entity
@Table("config")
public class Configuration implements serializable {
private Long id;
private List<ColumnGroup> columnGroups;
@Id
@GeneratedValue
@Column(name="ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@JoinTable(
name="config_column_group",
joinColumns=@JoinColumn(name="config_id", referencedColumnName="id"),
inverseJoinColumns=@JoinColumn(name="column_group_id", referencedColumnName="id"
)
List<ColumnGroup> getColumnGroups() {
return columnGroups;
}
public void setColumnGroups(List<ColumnGroup> columnGroups) {
this.columnGroups = columnGroups;
}
}
Entity 2:
@Entity
@Table("column_group")
public class ColumnGroup implements serializable {
private Long id;
private List<Column> columns;
@Id
@GeneratedValue
@Column("ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@OneToMany(cascade=CascadeType.ALL, mappedBy="columnGroup", fetch=FetchType.LAZY)
public List<Column> getColumns() {
return columns;
}
public void setColumns(List<Column> columns) {
this.columns = columns;
}
}
Then, I get an instance of the entity and call getColumnGroups():
Config config = (Config)HibernateManager.getSession().get(Config.class, id);
List<ColumnGroup> = config.getColumnGroups();
And I get the exception
org.hibernate.exception.SQLGrammarException: could not initialize a collection: [Configuration.columnGroups]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2173)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:627)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1863)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:369)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
...
Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "COLUMNGR1_"."CONFIG_ID": invalid identifier
And looking at the sql being run by hibernate, I see
select
columngrou0_.config_id as vt1_107_1_,
columngrou0_.column_group_id as column2_1_,
vtcolumngr1_.ID as ID109_0_,
vtcolumngr1_.CONFIG_ID as VT9_109_0_
from
config_column_group columngrou0_
inner join
COLUMN_GROUP vtcolumngr1_
on columngrou0_.column_group_id=columngr1_.ID
where
columngrou0_.vt_config_id=?
So it looks as if hibernate is trying to select a column that exists in the join table from the owning table, and it isn't selecting the target entity at all.
That would suggest to me that I didn't put the column names in the right place in the JoinTable/JoinColumn annotation, but I am following an example that works, and even if I do change things around, there doesn't seem to be any winning combination...
The tables are structured as you might expect-- config_column_group has id (pk), config_id (fk referencing config(id)), and column_id (fk referencing column_group(id)).
java hibernate one-to-many jointable
Your Configuration entity (table config) is joined with table config_column_group and not ColumnGroup (table column_group)
– Joakim Danielson
Nov 21 at 21:35
@JoakimDanielson, this is the pattern that I am following from working code... they have a one to many relationship with a second entity by joining to the join table... do you know how this should be done?
– user3337629
Nov 21 at 21:41
1
If you have an extra join table between the two then it looks like many-to-many relationship to me which you define differently than a one-to-many
– Joakim Danielson
Nov 22 at 5:55
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Simplified example of what's happening
Entity 1
@Entity
@Table("config")
public class Configuration implements serializable {
private Long id;
private List<ColumnGroup> columnGroups;
@Id
@GeneratedValue
@Column(name="ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@JoinTable(
name="config_column_group",
joinColumns=@JoinColumn(name="config_id", referencedColumnName="id"),
inverseJoinColumns=@JoinColumn(name="column_group_id", referencedColumnName="id"
)
List<ColumnGroup> getColumnGroups() {
return columnGroups;
}
public void setColumnGroups(List<ColumnGroup> columnGroups) {
this.columnGroups = columnGroups;
}
}
Entity 2:
@Entity
@Table("column_group")
public class ColumnGroup implements serializable {
private Long id;
private List<Column> columns;
@Id
@GeneratedValue
@Column("ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@OneToMany(cascade=CascadeType.ALL, mappedBy="columnGroup", fetch=FetchType.LAZY)
public List<Column> getColumns() {
return columns;
}
public void setColumns(List<Column> columns) {
this.columns = columns;
}
}
Then, I get an instance of the entity and call getColumnGroups():
Config config = (Config)HibernateManager.getSession().get(Config.class, id);
List<ColumnGroup> = config.getColumnGroups();
And I get the exception
org.hibernate.exception.SQLGrammarException: could not initialize a collection: [Configuration.columnGroups]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2173)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:627)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1863)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:369)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
...
Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "COLUMNGR1_"."CONFIG_ID": invalid identifier
And looking at the sql being run by hibernate, I see
select
columngrou0_.config_id as vt1_107_1_,
columngrou0_.column_group_id as column2_1_,
vtcolumngr1_.ID as ID109_0_,
vtcolumngr1_.CONFIG_ID as VT9_109_0_
from
config_column_group columngrou0_
inner join
COLUMN_GROUP vtcolumngr1_
on columngrou0_.column_group_id=columngr1_.ID
where
columngrou0_.vt_config_id=?
So it looks as if hibernate is trying to select a column that exists in the join table from the owning table, and it isn't selecting the target entity at all.
That would suggest to me that I didn't put the column names in the right place in the JoinTable/JoinColumn annotation, but I am following an example that works, and even if I do change things around, there doesn't seem to be any winning combination...
The tables are structured as you might expect-- config_column_group has id (pk), config_id (fk referencing config(id)), and column_id (fk referencing column_group(id)).
java hibernate one-to-many jointable
Simplified example of what's happening
Entity 1
@Entity
@Table("config")
public class Configuration implements serializable {
private Long id;
private List<ColumnGroup> columnGroups;
@Id
@GeneratedValue
@Column(name="ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@JoinTable(
name="config_column_group",
joinColumns=@JoinColumn(name="config_id", referencedColumnName="id"),
inverseJoinColumns=@JoinColumn(name="column_group_id", referencedColumnName="id"
)
List<ColumnGroup> getColumnGroups() {
return columnGroups;
}
public void setColumnGroups(List<ColumnGroup> columnGroups) {
this.columnGroups = columnGroups;
}
}
Entity 2:
@Entity
@Table("column_group")
public class ColumnGroup implements serializable {
private Long id;
private List<Column> columns;
@Id
@GeneratedValue
@Column("ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@OneToMany(cascade=CascadeType.ALL, mappedBy="columnGroup", fetch=FetchType.LAZY)
public List<Column> getColumns() {
return columns;
}
public void setColumns(List<Column> columns) {
this.columns = columns;
}
}
Then, I get an instance of the entity and call getColumnGroups():
Config config = (Config)HibernateManager.getSession().get(Config.class, id);
List<ColumnGroup> = config.getColumnGroups();
And I get the exception
org.hibernate.exception.SQLGrammarException: could not initialize a collection: [Configuration.columnGroups]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2173)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:627)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1863)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:369)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
...
Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "COLUMNGR1_"."CONFIG_ID": invalid identifier
And looking at the sql being run by hibernate, I see
select
columngrou0_.config_id as vt1_107_1_,
columngrou0_.column_group_id as column2_1_,
vtcolumngr1_.ID as ID109_0_,
vtcolumngr1_.CONFIG_ID as VT9_109_0_
from
config_column_group columngrou0_
inner join
COLUMN_GROUP vtcolumngr1_
on columngrou0_.column_group_id=columngr1_.ID
where
columngrou0_.vt_config_id=?
So it looks as if hibernate is trying to select a column that exists in the join table from the owning table, and it isn't selecting the target entity at all.
That would suggest to me that I didn't put the column names in the right place in the JoinTable/JoinColumn annotation, but I am following an example that works, and even if I do change things around, there doesn't seem to be any winning combination...
The tables are structured as you might expect-- config_column_group has id (pk), config_id (fk referencing config(id)), and column_id (fk referencing column_group(id)).
java hibernate one-to-many jointable
java hibernate one-to-many jointable
edited Nov 22 at 11:24
Billy Frost
1,72788
1,72788
asked Nov 21 at 21:03
user3337629
366
366
Your Configuration entity (table config) is joined with table config_column_group and not ColumnGroup (table column_group)
– Joakim Danielson
Nov 21 at 21:35
@JoakimDanielson, this is the pattern that I am following from working code... they have a one to many relationship with a second entity by joining to the join table... do you know how this should be done?
– user3337629
Nov 21 at 21:41
1
If you have an extra join table between the two then it looks like many-to-many relationship to me which you define differently than a one-to-many
– Joakim Danielson
Nov 22 at 5:55
add a comment |
Your Configuration entity (table config) is joined with table config_column_group and not ColumnGroup (table column_group)
– Joakim Danielson
Nov 21 at 21:35
@JoakimDanielson, this is the pattern that I am following from working code... they have a one to many relationship with a second entity by joining to the join table... do you know how this should be done?
– user3337629
Nov 21 at 21:41
1
If you have an extra join table between the two then it looks like many-to-many relationship to me which you define differently than a one-to-many
– Joakim Danielson
Nov 22 at 5:55
Your Configuration entity (table config) is joined with table config_column_group and not ColumnGroup (table column_group)
– Joakim Danielson
Nov 21 at 21:35
Your Configuration entity (table config) is joined with table config_column_group and not ColumnGroup (table column_group)
– Joakim Danielson
Nov 21 at 21:35
@JoakimDanielson, this is the pattern that I am following from working code... they have a one to many relationship with a second entity by joining to the join table... do you know how this should be done?
– user3337629
Nov 21 at 21:41
@JoakimDanielson, this is the pattern that I am following from working code... they have a one to many relationship with a second entity by joining to the join table... do you know how this should be done?
– user3337629
Nov 21 at 21:41
1
1
If you have an extra join table between the two then it looks like many-to-many relationship to me which you define differently than a one-to-many
– Joakim Danielson
Nov 22 at 5:55
If you have an extra join table between the two then it looks like many-to-many relationship to me which you define differently than a one-to-many
– Joakim Danielson
Nov 22 at 5:55
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420441%2fonetomany-relation-via-jointable-results-in-hibernate-selecting-invalid-column%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your Configuration entity (table config) is joined with table config_column_group and not ColumnGroup (table column_group)
– Joakim Danielson
Nov 21 at 21:35
@JoakimDanielson, this is the pattern that I am following from working code... they have a one to many relationship with a second entity by joining to the join table... do you know how this should be done?
– user3337629
Nov 21 at 21:41
1
If you have an extra join table between the two then it looks like many-to-many relationship to me which you define differently than a one-to-many
– Joakim Danielson
Nov 22 at 5:55