[
http://opensource.atlassian.com/projects/hibernate/browse/ANN-632?page=co...
]
Al Le commented on ANN-632:
---------------------------
This is all fine and documented for bidirectional associations. But what if I have an
unidirectional one with JoinColumn? The index column doesn't get set. I know this is
not recommended but we'd still like to use it.
Example:
class Parent {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "parent_id")
@IndexColumn(name = "child_index", nullable = false)
private List<Child> children = new ArrayList<Child>();
// Getters and Setters omitted
}
class Child {
// Some attributes not related to the association. There is no java field for the
child_index.
// But the DB table has this column (which does not allow NULL values).
}
In the code, I do something like this:
Parent parent = new Parent();
parent.getChildren().add(new Child("aChild"));
entityManager.save(parent);
In the generated SQL (for child insertion), the column 'child_index' does not even
occur. As a result, I have a DB constraint violation.
What am I doing wrong? Or is this really an issue in Hibernate?
@IndexColumn doesn't set value of index column
----------------------------------------------
Key: ANN-632
URL:
http://opensource.atlassian.com/projects/hibernate/browse/ANN-632
Project: Hibernate Annotations
Issue Type: Improvement
Components: documentation
Affects Versions: 3.3.0.ga
Reporter: Dan Allen
Assignee: Diego Plentz
Priority: Minor
I'm sure I will get screamed at for this, but the @IndexColumn just doesn't work
with @OneToMany. When I say it doesn't work, it means that I am a reasonable person
and I have studied the documentation for at least 4 hours and I just cannot figure out how
to make it work. So either the documentation needs to be improved, or there is something
wrong with Hibernate. I refuse to believe that I am this stupid.
Here is my problem in a nutshell. I have a Person and a collection of Jobs. The Jobs
should be an indexed list based on the history that the person holds them.
@Entity
public class Person {
@Id @GeneratedValue
private long id;
@Column
private String name;
@OneToMany(cascade=ALL, fetch=LAZY, mappedBy = "job")
@IndexColumn(base = 1, name = "order")
private List<Job> jobs = new ArrayList<Job>();
// getters and setters
}
@Entity
public class Job {
@Id @GeneratedValue
private long id;
@Column
private String name;
@ManyToOne
@JoinColumn(name="person_id")
private Person person;
@Column
private Integer order;
// getters and setters
}
If I do the following, I get NULL for order.
Person person = new Person();
person.setName("Chuck")
Job job1 = new Job();
job1.setName("sysadmin")
job1.setPerson(person);
person.getJobs().add(job1);
Job job2 = new Job();
jobs2.setName("network admin")
job2.setPerson(person);
person.getJobs().add(job2);
entityManager.persist(person);
Assume that the reason I am not assigning an order is more complex than this example. The
point is that we want to see the order column populated with the index of the list.
Now, if you give me the business about removing mappedBy, to that I will respond that by
removing mappedBy, Hibernate tries to work with a person_job table, which I don't
want. I want two tables, one for person and one for job.
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira