openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chris Pro" <chris-...@gmx.net>
Subject Bi-Directional Relation and Lazy
Date Mon, 24 Sep 2012 14:48:07 GMT
Hello

I've two JPA-Entities (OpenJPA 2.2.0): Task and TaskState.
TaskState have a foreign key on Task. Therefore I marked the relation TaskState.getTask()
as Lazy. The relation opposite Task.getTaskState() is Lazy too (Default for OneToMany). So
when I write a JPQL like that in an EJB both relations are filled: 
SELECT t FROM Task t LEFT JOIN FETCH t.TaskState

With that query both relations are filled. But the relation TaskState.getTask() should be
lazy and not filled. The problem is, that that query result-list-object is very big, because
of the bi-directional: Task have x TaksState -> TaskState have the Task -> Task have
x TaksState -> and so one. 

Is there a way, to prevent the loading/filling of TaskState.getTask()?

Would be great, if someone could help me.


@Entity
public class Task implements Serializable
{

    private static final long serialVersionUID = 1L;

    public Task()
    {}

	// ...
	
	private List<TaskState> TaskState;

    @OneToMany(mappedBy = "task")
    public List<TaskState> getTaskState()
    {
        return TaskState;
    }

    public void setTaskState(final List<TaskState> TaskState)
    {
        this.TaskState = TaskState;
    }	
}


@Entity
public class TaskState implements Serializable, Comparable<TaskState>
{
    private static final long serialVersionUID = 1L;

    public TaskState()
    {}
	
    private Task task;

    @ManyToOne(fetch = FetchType.LAZY)
    @javax.persistence.JoinColumn(name = "state_task_Id", referencedColumnName = "task_Id",
nullable = false)
    public Task getTask()
    {
        return task;
    }

    public void setTask(final Task task)
    {
        this.task = task;
    }	
}



// EJB
    public Collection<Task> findAllTasks() throws BBException
    {
        try
        {
            Query query = em.createQuery("SELECT t FROM Task t LEFT JOIN FETCH t.TaskState");
			Collection<Task> tasks = query.getResultList();
			return tasks;
			// Why are the tasks on TaskState.getTaskState() are also filled with Tasks-Objects?
        }
        catch (Exception ex)
		{
			throw new BBException(ex);
		}
	}			

Mime
View raw message