Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Using CopyGroup and CASCADE_TREE depth always get a null as copied entity
Using CopyGroup and CASCADE_TREE depth always get a null as copied entity [message #1043016] Wed, 17 April 2013 06:55 Go to next message
Carlos Salinas is currently offline Carlos SalinasFriend
Messages: 32
Registered: March 2011
Location: Avd. de la Argentina 132,...
Member
Hello All,

I have problem trying to clone an Entity named Service using CopyGroup and CASCADE_TREE depth. Whatever attribute i add to the CopyGroup i always get a null reference as the copied entity.

This is the Entity bean:

/*
 * Copyright 2011 Thingtrack, S.L.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.thingtrack.konekti.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.thingtrack.bustrack.domain.Route;
import com.thingtrack.bustrack.domain.ServiceTemplate;
import com.thingtrack.bustrack.domain.Turn;
import com.thingtrack.bustrack.domain.VehicleType;
import com.thingtrack.bustrack.domain.WorksheetLine;

/**
 * @author Thingtrack S.L.
 * 
 */
@SuppressWarnings("serial")
@Entity
@Table(name = "SERVICE")
public class Service implements Serializable {
	@Id
	@Column(name = "SERVICE_ID")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer serviceId;

	@ManyToOne
	@JoinColumn(name = "ORGANIZATION_ID", nullable = false)
	private Organization organization;

	@Column(name = "CODE", nullable = false, unique = true, length = 64)
	private String code;

	@Column(name = "DESCRIPTION", length = 512)
	private String description;

	@ManyToOne
	@JoinColumn(name = "SERVICE_TYPE_ID", nullable = false)
	private ServiceType serviceType;

	@ManyToOne
	@JoinColumn(name = "CLIENT_ID")
	private Client client;

	@ManyToOne
	@JoinColumn(name = "TURN_ID")
	private Turn turn;

	@ManyToOne
	@JoinColumn(name = "OFFER_LINE_ID")
	private OfferLine offerLine;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "INVOICE_LINE_ID")
	private InvoiceLine invoiceLine;

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "service")
	private List<WorksheetLine> worksheetLines = new ArrayList<WorksheetLine>();

	@ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST,
			CascadeType.MERGE, CascadeType.REFRESH })
	@JoinTable(name = "SERVICE_ROUTE", joinColumns = @JoinColumn(name = "SERVICE_ID"), inverseJoinColumns = @JoinColumn(name = "ROUTE_ID"))
	@OrderColumn(name = "ROUTE_ORDER")
	private List<Route> routes = new ArrayList<Route>();

	@Column(name = "INTERMEDIATE_STOPS")
	private String intermediateStops;

	@Column(name = "STOP_NUMBER")
	private Integer stopNumber;

	@Column(name = "PASSENGERS")
	private Integer passengers;

	@Column(name = "DRIVER_RPM_QUALITY", length = 10, precision = 2)
	private double driverRPMQuality;

	@Column(name = "KM_OFFER", length = 10, precision = 2)
	private double kmOffer;

	@Column(name = "KM_REAL", length = 10, precision = 2)
	private double kmReal;

	@Column(name = "GAS_OFFER", length = 10, precision = 2)
	private double gasOffer;

	@Column(name = "GAS_REAL", length = 10, precision = 2)
	private double gasReal;

	@Column(name = "START_SERVICE")
	@Temporal(TemporalType.TIMESTAMP)
	private Date startService;

	@Column(name = "STOP_SERVICE")
	@Temporal(TemporalType.TIMESTAMP)
	private Date stopService;

	@ManyToOne
	@JoinColumn(name = "VEHICLE_TYPE_ID")
	private VehicleType vehicleType;

	@Column(name = "LUNCH", nullable = false)
	private boolean lunch = false;

	@Column(name = "DINNER", nullable = false)
	private boolean dinner = false;

	@Column(name = "BREACKFAST", nullable = false)
	private boolean breackfast = false;

	@Column(name = "ACCOMODATION", nullable = false)
	private boolean accomodation = false;

	@Column(name = "OBSERVATION", length = 512)
	private String observation;

	@Column(name = "RESERVATION_DATE", nullable = false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date reservationDate = new Date();

	@Column(name = "SCHEDULE_DATE")
	@Temporal(TemporalType.TIMESTAMP)
	private Date scheduleDate;

	@ManyToOne(optional = false)
	@JoinColumn(name = "SERVICE_STATUS_ID", nullable = false)
	private ServiceStatus serviceStatus;

	@Column(name = "ROUTE_TYPE")
	private String routeType;

	@Column(name = "ROUTE_AVOIDS")
	private String routeAvoids;

	@ManyToOne(optional = false)
	@JoinColumn(name = "SERVICE_TEMPLATE_ID", nullable = false)
	private ServiceTemplate serviceTemplate;

	@ManyToMany(mappedBy = "services")
	private List<CalendarGroup> calendarGroups;

	@Column(name = "TEMPLATE")
	private boolean template;
	
	@Column(name="COLOR", nullable=false, length=64)
	private String color;

	public enum STATUS {
		OPENED, DESIGNED, PLANNED, RUNNING, PAUSED, REJECTED, CLOSED
	}

	public enum TYPE {
		OTHER
	}

	public enum ROUTE_TYPE {
		FASTEST, SHORTEST
	}

	public enum ROUTE_AVOIDS {
		AVOID_HIGHWAYS('H'), AVOID_TOLLS('T'), AVOID_UNPAVED('U'), AVOID_FERRIES(
				'F'), AVOID_COUNTRY_BORDERS('C'), AVOID_SEASONAL_ROADS('R'), AVOID_TIMED_RESTRICTIONS(
				'I');

		private char value;

		public char getValue() {
			return value;
		}

		ROUTE_AVOIDS(char value) {
			this.value = value;
		}
	}

	public Service() {

	}

	public Service(String code, Date reservationDate,
			Organization organization, ServiceType serviceType,
			ServiceStatus serviceStatus) {
		this.code = code;
		this.reservationDate = reservationDate;
		this.organization = organization;
		this.serviceType = serviceType;
		this.serviceStatus = serviceStatus;

	}

	/**
	 * @param serviceId
	 *            the serviceId to set
	 */
	public void setServiceId(Integer serviceId) {
		this.serviceId = serviceId;
	}

	/**
	 * @return the serviceId
	 */
	public Integer getServiceId() {
		return serviceId;
	}

	/**
	 * @param code
	 *            the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the serviceType
	 */
	public ServiceType getServiceType() {
		return serviceType;
	}

	/**
	 * @param serviceType
	 *            the serviceType to set
	 */
	public void setServiceType(ServiceType serviceType) {
		this.serviceType = serviceType;
	}

	/**
	 * @return the client
	 */
	public Client getClient() {
		return client;
	}

	/**
	 * @param client
	 *            the client to set
	 */
	public void setClient(Client client) {
		this.client = client;
	}

	/**
	 * @return the reservationDate
	 */
	public Date getReservationDate() {
		return reservationDate;
	}

	/**
	 * @param reservationDate
	 *            the reservationDate to set
	 */
	public void setReservationDate(Date reservationDate) {
		this.reservationDate = reservationDate;
	}

	/**
	 * @return the scheduleDate
	 */
	public Date getScheduleDate() {
		return scheduleDate;
	}

	/**
	 * @param scheduleDate
	 *            the scheduleDate to set
	 */
	public void setScheduleDate(Date scheduleDate) {
		this.scheduleDate = scheduleDate;
	}

	/**
	 * @return the serviceStatus
	 */
	public ServiceStatus getServiceStatus() {
		return serviceStatus;
	}

	/**
	 * @param serviceStatus
	 *            the serviceStatus to set
	 */
	public void setServiceStatus(ServiceStatus serviceStatus) {
		this.serviceStatus = serviceStatus;
	}

	public String getRouteType() {
		return routeType;
	}

	public void setRouteType(String routeType) {
		this.routeType = routeType;
	}

	public String getRouteAvoids() {
		return routeAvoids;
	}

	public void setRouteAvoids(String routeAvoids) {
		this.routeAvoids = routeAvoids;
	}

	/**
	 * @param offerLine
	 *            the offerLine to set
	 */
	public void setOfferLine(OfferLine offerLine) {
		this.offerLine = offerLine;
	}

	/**
	 * @return the offerLine
	 */
	public OfferLine getOfferLine() {
		return offerLine;
	}

	/**
	 * @param organization
	 *            the organization to set
	 */
	public void setOrganization(Organization organization) {
		this.organization = organization;
	}

	/**
	 * @return the organization
	 */
	public Organization getOrganization() {
		return organization;
	}

	/**
	 * @return the worksheetLine
	 */
	public List<WorksheetLine> getWorksheetLines() {
		return Collections.unmodifiableList(worksheetLines);
	}

	/**
	 * @param worksheetLine
	 *            the worksheetLine to set
	 */
	public void setWorksheetLines(List<WorksheetLine> worksheetLines) {

		// Clean all worksheet lines
		removeAllWorksheetLines();

		// Insert the new ones
		for (WorksheetLine worksheetLine : worksheetLines)
			addWorksheetLine(worksheetLine);
	}

	public void addWorksheetLine(WorksheetLine worksheetLine) {

		if (worksheetLines.contains(worksheetLine))
			return;

		worksheetLine.setService(this);
		worksheetLines.add(worksheetLine);
	}

	public void removeWorksheetLine(WorksheetLine worksheetLine) {

		if (worksheetLines.remove(worksheetLine))
			worksheetLine.setService(null);
	}

	public void removeAllWorksheetLines() {
		for (WorksheetLine worksheetLine : worksheetLines)
			worksheetLine.setService(null);

		worksheetLines.clear();
	}

	/**
	 * @return the observation
	 */
	public String getObservation() {
		return observation;
	}

	/**
	 * @param observation
	 *            the observation to set
	 */
	public void setObservation(String observation) {
		this.observation = observation;
	}

	/**
	 * @return the routes
	 */
	public List<Route> getRoutes() {
		return Collections.unmodifiableList(routes);
	}

	public void setRoutes(List<Route> routes) {

		removeAllRoutes();

		for (Route route : routes) {
			addRoute(route);
		}
	}

	public void removeAllRoutes() {

		for (Route route : this.routes)
			if (route.getServices().contains(this))
				route.removeService(this);

		routes.clear();
	}

	/**
	 * @return the stopNumber
	 */
	public Integer getStopNumber() {
		return stopNumber;
	}

	/**
	 * @param stopNumber
	 *            the stopNumber to set
	 */
	public void setStopNumber(Integer stopNumber) {
		this.stopNumber = stopNumber;
	}

	/**
	 * @return the passengers
	 */
	public Integer getPassengers() {
		return passengers;
	}

	/**
	 * @param passengers
	 *            the passengers to set
	 */
	public void setPassengers(Integer passengers) {
		this.passengers = passengers;
	}

	/**
	 * @return the driverRPMQuality
	 */
	public double getDriverRPMQuality() {
		return driverRPMQuality;
	}

	/**
	 * @param driverRPMQuality
	 *            the driverRPMQuality to set
	 */
	public void setDriverRPMQuality(double driverRPMQuality) {
		this.driverRPMQuality = driverRPMQuality;
	}

	/**
	 * @return the kmOffer
	 */
	public double getKmOffer() {
		return kmOffer;
	}

	/**
	 * @param kmOffer
	 *            the kmOffer to set
	 */
	public void setKmOffer(double kmOffer) {
		this.kmOffer = kmOffer;
	}

	/**
	 * @return the kmReal
	 */
	public double getKmReal() {
		return kmReal;
	}

	/**
	 * @param kmReal
	 *            the kmReal to set
	 */
	public void setKmReal(double kmReal) {
		this.kmReal = kmReal;
	}

	/**
	 * @return the gasOffer
	 */
	public double getGasOffer() {
		return gasOffer;
	}

	/**
	 * @param gasOffer
	 *            the gasOffer to set
	 */
	public void setGasOffer(double gasOffer) {
		this.gasOffer = gasOffer;
	}

	/**
	 * @return the gasReal
	 */
	public double getGasReal() {
		return gasReal;
	}

	/**
	 * @param gasReal
	 *            the gasReal to set
	 */
	public void setGasReal(double gasReal) {
		this.gasReal = gasReal;
	}

	/**
	 * @return the startService
	 */
	public Date getStartService() {
		return startService;
	}

	/**
	 * @param startService
	 *            the startService to set
	 */
	public void setStartService(Date startService) {
		this.startService = startService;
	}

	/**
	 * @param turn
	 *            the turn to set
	 */
	public void setTurn(Turn turn) {
		this.turn = turn;
	}

	/**
	 * @return the turn
	 */
	public Turn getTurn() {
		return turn;
	}

	/**
	 * @param invoiceLine
	 *            the invoiceLine to set
	 */
	public void setInvoiceLine(InvoiceLine invoiceLine) {
		this.invoiceLine = invoiceLine;
	}

	/**
	 * @return the invoiceLine
	 */
	public InvoiceLine getInvoiceLine() {
		return invoiceLine;
	}

	/**
	 * @param stopService
	 *            the stopService to set
	 */
	public void setStopService(Date stopService) {
		this.stopService = stopService;
	}

	/**
	 * @return the stopService
	 */
	public Date getStopService() {
		return stopService;
	}

	public void addRoute(Route route) {

		routes.add(route);

		if (!route.getServices().contains(this))
			route.addService(this);

	}

	public void removeRoute(Route route) {

		routes.remove(route);

		if (route.getServices().contains(this))
			route.removeService(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((code == null) ? 0 : code.hashCode());
		result = prime * result
				+ ((serviceId == null) ? 0 : serviceId.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {

		// Type check
		if (!(obj instanceof Service))
			return false;
		Service other = (Service) obj;

		if (serviceId != null && serviceId.equals(other.serviceId))
			return true;

		// Routes check
		if (this.routes != other.routes)
			return false;

		if (!this.routes.containsAll(other.routes))
			return false;

		// Code check
		if (code == null && other.code != null)
			return false;

		if (!code.equals(other.code))
			return false;

		// Service Id check
		if (serviceId == null && other.serviceId != null)
			return false;

		return super.equals(obj);
	}

	@Override
	public String toString() {
		return "Service [serviceId=" + serviceId + ", code=" + code + "]";

	}

	/**
	 * @return the intermediateStops
	 */
	public String getIntermediateStops() {
		return intermediateStops;
	}

	/**
	 * @param intermediateStops
	 *            the intermediateStops to set
	 */
	public void setIntermediateStops(String intermediateStops) {
		this.intermediateStops = intermediateStops;
	}

	/**
	 * @return the lunch
	 */
	public boolean isLunch() {
		return lunch;
	}

	/**
	 * @param lunch
	 *            the lunch to set
	 */
	public void setLunch(boolean lunch) {
		this.lunch = lunch;
	}

	/**
	 * @return the dinner
	 */
	public boolean isDinner() {
		return dinner;
	}

	/**
	 * @param dinner
	 *            the dinner to set
	 */
	public void setDinner(boolean dinner) {
		this.dinner = dinner;
	}

	/**
	 * @return the accomodation
	 */
	public boolean isAccomodation() {
		return accomodation;
	}

	/**
	 * @param accomodation
	 *            the accomodation to set
	 */
	public void setAccomodation(boolean accomodation) {
		this.accomodation = accomodation;
	}

	/**
	 * @return the breackfast
	 */
	public boolean isBreackfast() {
		return breackfast;
	}

	/**
	 * @param breackfast
	 *            the breackfast to set
	 */
	public void setBreackfast(boolean breackfast) {
		this.breackfast = breackfast;
	}

	/**
	 * @return the vehicleType
	 */
	public VehicleType getVehicleType() {
		return vehicleType;
	}

	/**
	 * @param vehicleType
	 *            the vehicleType to set
	 */
	public void setVehicleType(VehicleType vehicleType) {
		this.vehicleType = vehicleType;
	}
	
	public List<CalendarGroup> getCalendarGroups() {
		return Collections.unmodifiableList(calendarGroups);
	}

	public void setCalendarGroups(List<CalendarGroup> calendarGroups) {
		this.calendarGroups = calendarGroups;
	}

	public void addCalendarGroup(CalendarGroup calendarGroup) {
		this.calendarGroups.add(calendarGroup);
	}

	public void removeCalendarGroup(CalendarGroup calendarGroup) {
		this.calendarGroups.remove(calendarGroup);
	}
	
	public void removeAllCalendarGroups(){
		for(CalendarGroup calendarGroup : this.calendarGroups)
			calendarGroup.removesServiceUnidirectional(this);
		
		this.calendarGroups.clear();
	}

	/**
	 * @return the serviceTemplate
	 */
	public ServiceTemplate getServiceTemplate() {
		return serviceTemplate;
	}

	/**
	 * @param serviceTemplate
	 *            the serviceTemplate to set
	 */
	public void setServiceTemplate(ServiceTemplate serviceTemplate) {
		this.serviceTemplate = serviceTemplate;
	}

	/**
	 * @return the template
	 */
	public boolean isTemplate() {
		return template;
	}

	/**
	 * @param template
	 *            the template to set
	 */
	public void setTemplate(boolean template) {
		this.template = template;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

}



And here there is DAO method:

@Override
	public Service copy(Service service) throws Exception {
		
		// Instantiate collections
		CopyGroup copyGroup = new CopyGroup();
		copyGroup.setShouldResetPrimaryKey(true);
		copyGroup.addAttribute("code");
		copyGroup.addAttribute("description");
		copyGroup.addAttribute("organization");
		
		
		Service copiedService = (Service) getEntityManager().unwrap(JpaEntityManager.class).copy(service, copyGroup);
		return copiedService;
	}


The method is called within a transaction and the 'copiedService is always null.

Any idea what i am doing wrong.
Thanks

P.D. : I am using EclipseLink 2.30, Gemini JPA 1.0.0 And Virgo 3.0.3

[Updated on: Wed, 17 April 2013 07:00]

Report message to a moderator

Re: Using CopyGroup and CASCADE_TREE depth always get a null as copied entity [message #1043336 is a reply to message #1043016] Wed, 17 April 2013 15:18 Go to previous messageGo to next message
Shaun Smith is currently offline Shaun SmithFriend
Messages: 197
Registered: July 2009
Senior Member
Hi Carlos,

Is weaving enabled? Weaving is necessary for fetch group/copy group usage. I know weaving is supported by Gemini using the OSGi standard weaving infrastructure but I don't know if Virgo supports this.

--Shaun
Re: Using CopyGroup and CASCADE_TREE depth always get a null as copied entity [message #1043350 is a reply to message #1043336] Wed, 17 April 2013 15:38 Go to previous messageGo to next message
Carlos Salinas is currently offline Carlos SalinasFriend
Messages: 32
Registered: March 2011
Location: Avd. de la Argentina 132,...
Member
Hi Shaun,

Firstly thank for your reply. The CopyGroup using CASCADE_ALL_PARTS and CASCADE_PRIVATE_PART works. So the unique 'depth mode' doest work is CASCADE_TREE.
We already using weaving implement our Transactional model.
Re: Using CopyGroup and CASCADE_TREE depth always get a null as copied entity [message #1043502 is a reply to message #1043350] Wed, 17 April 2013 19:53 Go to previous messageGo to next message
Andrei Ilitchev is currently offline Andrei IlitchevFriend
Messages: 5
Registered: July 2009
Junior Member
Please verify that the entity is weaved to support FetchGroups:
if ((new Service()) instanceof FetchGroupTracker) {
System.out.println("Supports fetch groups");
}
Re: Using CopyGroup and CASCADE_TREE depth always get a null as copied entity [message #1043855 is a reply to message #1043502] Thu, 18 April 2013 07:41 Go to previous messageGo to next message
Carlos Salinas is currently offline Carlos SalinasFriend
Messages: 32
Registered: March 2011
Location: Avd. de la Argentina 132,...
Member
Hi Andrei,

I check the snippet u wrote and and test doest enter in the conditional block. So i have a doubt reading the FetchGroupTracker javadoc.
Do i have to implement in my domain entities the FetchGroupTracker interface to use CASCADE_TREE in CopyGroup? in the EclipseLink Examples pages doest tell anything about it.
Re: Using CopyGroup and CASCADE_TREE depth always get a null as copied entity [message #1044101 is a reply to message #1043855] Thu, 18 April 2013 13:44 Go to previous message
Andrei Ilitchev is currently offline Andrei IlitchevFriend
Messages: 5
Registered: July 2009
Junior Member
Hi Carlos,

If FetchGroupTracker interface is not implemented by your entity, then it wasn't weaved for FecthGroups. Weaving for fetch groups occurs automatically if javaagent is used - but gor some reason it didn't happen in your case. It's a configuration issue.

To see if any Eclipselink weaving has been applied to the entity print out all the interfaces it implements:

for (Class cls : Sevice.class.getInterfaces()) {
System.out.println("\t\t" + cls.getName());
}

If the printout doesn't include PersistenceWeaved interface then the entity hasn't been weaved by Eclipselink at all.
Previous Topic:One to many and Many to One MOXy @XmlInverseReference EclipseLink 2.5
Next Topic:JPA with extended objects
Goto Forum:
  


Current Time: Tue Apr 23 09:29:57 GMT 2024

Powered by FUDForum. Page generated in 0.03325 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top