Quantcast
Channel: Apache Timeline
Viewing all articles
Browse latest Browse all 5648

Fetchgroups usage

$
0
0
Hi, I have a question about fetch groups in openJPA.

I have two entities: User and Device.

User:
@Entity
public class CmsUser extends CmsObject {

@Basic
private String name;

@Basic
private String login;

//search for join table field as list<long>
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "user_id", referencedColumnName = "id")
private List<Device> devices;

public CmsUser(String name, String login) {
this.name = name;
this.login = login;

public String getName() {
return name;

public void setName(String name) {
this.name = name;

public String getLogin() {
return login;

public void setLogin(String login) {
this.login = login;

public List<Device> getDevices() {
return devices;

public void setDevices(List<Device> devices) {
this.devices = devices;

public CmsUser() {

@Override
public String toString() {
return "id = " + getId()
+ ", login = " + getLogin()
+ ", devices = " + getDevices();

Device :

@Entity
@FetchGroup(name = "flat", attributes = {@FetchAttribute(name = "deviceId")})
public class Device extends CmsObject {

@Basic
private String directoryNumber;

@Basic
private String deviceId;

@Enumerated(EnumType.STRING)
private CmsType type;

public Device(String directoryNumber, String deviceId, CmsType type) {
this.directoryNumber = directoryNumber;
this.deviceId = deviceId;
this.type = type;

public String getDirectoryNumber() {
return directoryNumber;

public void setDirectoryNumber(String directoryNumber) {
this.directoryNumber = directoryNumber;

public String getDeviceId() {
return deviceId;

public void setDeviceId(String deviceId) {
this.deviceId = deviceId;

public CmsType getType() {
return type;

public void setType(CmsType type) {
this.type = type;

public Device() {

@Override
public String toString() {
return "id = " + getId()
+ ", number = " + getDirectoryNumber()
+ ", type = " + getType();

SuperClass Object:
@MappedSuperclass
public abstract class CmsObject {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

public Long getId() {
return id;

public void setId(Long id) {
this.id = id;

How can I implement feature that loads User with devices which contains only id field?

I have test class that loads users. When user's devices fetch=lazy users.device=null. And when fetch=eager It loads all device fields.

Viewing all articles
Browse latest Browse all 5648

Trending Articles