openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Wolf <cwolf.a...@gmail.com>
Subject Re: cannot persist m2m data, getting "ArgumentException: Attempt to cast instance xxx [java.util.HashSet]" to PersistenceCapable failed. Ensure that it has been enhanced."
Date Mon, 19 Nov 2012 15:02:47 GMT
Thanks John,

Here they are:

package persistdemo.ojpa.entities;

import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.*;


/**
 * The persistent class for the MARKET_DATA database table.
 *
 */
@Entity
@Table(name="MARKET_DATA")
public class MarketData implements Serializable {
	private static final long serialVersionUID = 1L;
	private long marketDataId;
	private String assetStatus;
	private BigDecimal callable;
	private BigDecimal convertible;
	private String currencyCode;
	private BigDecimal floating;
	private BigDecimal lastPrice;
	private BigDecimal lastUpdatedPrice;
	private String name;
	private String partnerContext;
	private BigDecimal puttable;
	private String rating;
	private String requestId;
	private String requestIdType;
	private BigDecimal sinkable;
	private String statusDescription;
	private String subType;
	private String type;
	private String vendorId;
	private String vendorName;
	private List<RiskFactor> riskFactors;

    public MarketData() {
    }


	@Id
	@SequenceGenerator(name="MARKET_DATA_MARKETDATAID_GENERATOR",
sequenceName="MARKET_DATA_ID_SEQ")
	@GeneratedValue(strategy=GenerationType.SEQUENCE,
generator="MARKET_DATA_MARKETDATAID_GENERATOR")
	@Column(name="MARKET_DATA_ID")
	public long getMarketDataId() {
		return this.marketDataId;
	}

	public void setMarketDataId(long marketDataId) {
		this.marketDataId = marketDataId;
	}


	@Column(name="ASSET_STATUS")
	public String getAssetStatus() {
		return this.assetStatus;
	}

	public void setAssetStatus(String assetStatus) {
		this.assetStatus = assetStatus;
	}


	public BigDecimal getCallable() {
		return this.callable;
	}

	public void setCallable(BigDecimal callable) {
		this.callable = callable;
	}


	public BigDecimal getConvertible() {
		return this.convertible;
	}

	public void setConvertible(BigDecimal convertible) {
		this.convertible = convertible;
	}


	@Column(name="CURRENCY_CODE")
	public String getCurrencyCode() {
		return this.currencyCode;
	}

	public void setCurrencyCode(String currencyCode) {
		this.currencyCode = currencyCode;
	}


	public BigDecimal getFloating() {
		return this.floating;
	}

	public void setFloating(BigDecimal floating) {
		this.floating = floating;
	}


	@Column(name="LAST_PRICE")
	public BigDecimal getLastPrice() {
		return this.lastPrice;
	}

	public void setLastPrice(BigDecimal lastPrice) {
		this.lastPrice = lastPrice;
	}


	@Column(name="LAST_UPDATED_PRICE")
	public BigDecimal getLastUpdatedPrice() {
		return this.lastUpdatedPrice;
	}

	public void setLastUpdatedPrice(BigDecimal lastUpdatedPrice) {
		this.lastUpdatedPrice = lastUpdatedPrice;
	}


	public String getName() {
		return this.name;
	}

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


	@Column(name="PARTNER_CONTEXT")
	public String getPartnerContext() {
		return this.partnerContext;
	}

	public void setPartnerContext(String partnerContext) {
		this.partnerContext = partnerContext;
	}


	public BigDecimal getPuttable() {
		return this.puttable;
	}

	public void setPuttable(BigDecimal puttable) {
		this.puttable = puttable;
	}


	public String getRating() {
		return this.rating;
	}

	public void setRating(String rating) {
		this.rating = rating;
	}


	@Column(name="REQUEST_ID")
	public String getRequestId() {
		return this.requestId;
	}

	public void setRequestId(String requestId) {
		this.requestId = requestId;
	}


	@Column(name="REQUEST_ID_TYPE")
	public String getRequestIdType() {
		return this.requestIdType;
	}

	public void setRequestIdType(String requestIdType) {
		this.requestIdType = requestIdType;
	}


	public BigDecimal getSinkable() {
		return this.sinkable;
	}

	public void setSinkable(BigDecimal sinkable) {
		this.sinkable = sinkable;
	}


	@Column(name="STATUS_DESCRIPTION")
	public String getStatusDescription() {
		return this.statusDescription;
	}

	public void setStatusDescription(String statusDescription) {
		this.statusDescription = statusDescription;
	}


	@Column(name="SUB_TYPE")
	public String getSubType() {
		return this.subType;
	}

	public void setSubType(String subType) {
		this.subType = subType;
	}


	@Column(name="\"TYPE\"")
	public String getType() {
		return this.type;
	}

	public void setType(String type) {
		this.type = type;
	}


	@Column(name="VENDOR_ID")
	public String getVendorId() {
		return this.vendorId;
	}

	public void setVendorId(String vendorId) {
		this.vendorId = vendorId;
	}


	@Column(name="VENDOR_NAME")
	public String getVendorName() {
		return this.vendorName;
	}

	public void setVendorName(String vendorName) {
		this.vendorName = vendorName;
	}


	//bi-directional many-to-many association to RiskFactor
    @ManyToMany
	@JoinTable(
		name="MARKET_DATA_RISK_FACTOR"
		, joinColumns={
			@JoinColumn(name="MARKET_DATA_ID")
			}
		, inverseJoinColumns={
			@JoinColumn(name="RISK_FACTOR_ID")
			}
		)
	public List<RiskFactor> getRiskFactors() {
		return this.riskFactors;
	}

	public void setRiskFactors(List<RiskFactor> riskFactors) {
		this.riskFactors = riskFactors;
	}
	
}


package persistdemo.ojpa.entities;

import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.*;


/**
 * The persistent class for the RISK_FACTOR database table.
 *
 */
@Entity
@Table(name="RISK_FACTOR")
public class RiskFactor implements Serializable {
	private static final long serialVersionUID = 1L;
	private long riskFactorId;
	private String name;
	private BigDecimal seq;
	private String type;
	private BigDecimal value;
	private List<MarketData> marketData;

    public RiskFactor() {
    }


	@Id
	@SequenceGenerator(name="RISK_FACTOR_RISKFACTORID_GENERATOR",
sequenceName="RISK_FACTOR_ID_SEQ")
	@GeneratedValue(strategy=GenerationType.SEQUENCE,
generator="RISK_FACTOR_RISKFACTORID_GENERATOR")
	@Column(name="RISK_FACTOR_ID")
	public long getRiskFactorId() {
		return this.riskFactorId;
	}

	public void setRiskFactorId(long riskFactorId) {
		this.riskFactorId = riskFactorId;
	}


	public String getName() {
		return this.name;
	}

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


	public BigDecimal getSeq() {
		return this.seq;
	}

	public void setSeq(BigDecimal seq) {
		this.seq = seq;
	}


	@Column(name="\"TYPE\"")
	public String getType() {
		return this.type;
	}

	public void setType(String type) {
		this.type = type;
	}


	@Column(name="\"VALUE\"")
	public BigDecimal getValue() {
		return this.value;
	}

	public void setValue(BigDecimal value) {
		this.value = value;
	}


	//bi-directional many-to-many association to MarketData
	@ManyToMany(mappedBy="riskFactors")
	public List<MarketData> getMarketData() {
		return this.marketData;
	}

	public void setMarketData(List<MarketData> marketData) {
		this.marketData = marketData;
	}
	
}

On Mon, Nov 19, 2012 at 7:37 AM, Boblitz John <John.Boblitz@bertschi.com> wrote:
> Hello Chris,
>
> Just out of curiosity, do you have RiskFactor defined with @Entity?
>
> Maybe a complete listing of the RiskFactor & MarketData might help further ....
>
>
> John
>
>> -----Urspr√ľngliche Nachricht-----
>> Von: Chris Wolf [mailto:cwolf.algo@gmail.com]
>> Gesendet: Sonntag, 18. November 2012 16:37
>> An: users@openjpa.apache.org
>> Betreff: cannot persist m2m data, getting "ArgumentException:
>> Attempt to cast instance xxx [java.util.HashSet]" to
>> PersistenceCapable failed. Ensure that it has been enhanced."
>>
>> Hello,
>>
>> This is my first posting and first attempt to use OpenJPA.  I
>> put together a quick demo and can persist individual,
>> unrelated entities.  However, when I try to persist related
>> entities to two tables via a link table, i.e. many-2-many, it
>> keeps complaining about "casting to PersistenceCapable", in
>> particular the class "java.util.HashSet".
>>
>> First, I am using the Eclipse JPA plugin (called "Dali" or
>> "EclipseLink").  Of course, I have OpenJPA configured as my
>> JPA provider, I am in a plain Java SE environment with
>> LOCAL_RESOURCE via JDBC connection properties in the
>> persistence.xml.  I am using Sun/Oracle 64bit
>> JDK-1.6 and OpenJPA-2.2.0.
>>
>> I am using the Eclipse JPA plugin to generate the entity
>> classes from already-exiting database schema objects, and
>> that code looks like (just pasting the relationship code),
>> this action also adds these classes to persistence.xml via
>> persistence-unit/class elements.
>>
>> First M2M entity, "MarketData":
>>
>>       //bi-directional many-to-many association to RiskFactor
>>       @ManyToMany(mappedBy="marketData")
>>       public Set<RiskFactor> getRiskFactors() {
>>               return this.riskFactors;
>>       }
>>
>> Second M2M entity "RiskFactor":
>>
>>       //bi-directional many-to-many association to MarketData
>>     @ManyToMany
>>       @JoinTable(
>>               name="MARKET_DATA__RISK_FACTOR"
>>               , joinColumns={
>>                       @JoinColumn(name="RISK_FACTOR_ID",
>> nullable=false)
>>                       }
>>               , inverseJoinColumns={
>>                       @JoinColumn(name="MARKET_DATA_ID",
>> nullable=false)
>>                       }
>>               )
>>       public Set<MarketData> getMarketData() {
>>               return this.marketData;
>>       }
>>
>> When I run the code, the log indicates implicit runtime
>> enhacement, yet it is complaining:
>>
>> "[persistdemo.ojpa.entities.RiskFactor@61578aab] [java.util.HashSet]"
>> to PersistenceCapable failed.  Ensure that it has been enhanced."
>>
>> ...when it says, "Ensure that it has been enhanced." - which is "it"
>> referring to?  The entity "RiskFactor" or the field
>> relationship field's class, "java.util.HashSet"?
>>
>>
>> 186  openjpa  INFO   [main] openjpa.Runtime - OpenJPA dynamically
>> loaded the class enhancer. Any classes that were not enhanced
>> at build time will be enhanced when they are loaded by the JVM.
>> SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
>> SLF4J: Defaulting to no-operation (NOP) logger implementation
>> SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder
>> for further details.
>> 243  openjpa  INFO   [main] openjpa.Runtime - OpenJPA dynamically
>> loaded a validation provider.
>> 596  openjpa  INFO   [main] openjpa.Runtime - Starting OpenJPA 2.2.0
>> 630  openjpa  INFO   [main] openjpa.jdbc.JDBC - Using dictionary class
>> "org.apache.openjpa.jdbc.sql.OracleDictionary".
>> Exception in thread "main" <openjpa-2.2.0-r422266:1244990
>> nonfatal user error>
>> org.apache.openjpa.persistence.ArgumentException: Attempt to
>> cast instance "[persistdemo.ojpa.entities.RiskFactor@61578aab]
>> [java.util.HashSet]" to PersistenceCapable failed.  Ensure
>> that it has been enhanced.
>>
>> Maybe because "java.util.HashSet" was loaded before the
>> dynamic enhancer could get to it?
>>
>> Next, I tried performing build-time enhancement via Maven,
>> per this doc:
>> http://openjpa.apache.org/enhancement-with-maven.html
>>
>> When I ran "mvn openjpa:enhance", it finished with success,
>> but none of the classes in target/classes seemed to have be
>> changed (last-modified date same as compile-time).  and
>> re-running results in the same error and stack-trace.
>>
>> My openjpa:enhance configuration was:
>> <configuration>
>>     <includes>**/entities/*.class,java.util.HashSet</includes>
>>      <addDefaultConstructor>true</addDefaultConstructor>
>>      <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
>> </configuration>
>>
>>
>> Next, I tried invoking with:
>>
>> -javaagent:/opt/apache-openjpa-2.2.0/openjpa-all-2.2.0.jar
>>
>> Same error - same stack trace.
>>
>> Then, I tried setting this property:
>> openjpa.RuntimeUnenhancedClasses=supported
>>
>> Same error - same stack trace.
>>
>>
>> Then, following a suggestion I found here:
>> http://openjpa.208410.n2.nabble.com/JPA-adding-entities-to-Ent
>> ityManagerFactory-programmatically-td210697.html
>>
>> I tried setting both:
>> openjpa.RuntimeUnenhancedClasses=supported
>> openjpa.MetaDataFactory=jpa(Types=java.util.HashSet)
>>
>> BTW, this is a dead link
>> "User's Guide on Enhancement" /
>> http://openjpa.apache.org/builds/latest/docs/manual/manual.htm
>> l#ref_guide_pc_enhance
>> (from page: http://openjpa.apache.org/entity-enhancement.html)
>>
>> So is there any way to use OpenJPA to persist objects related
>> via a link table?  (there obviously must be, but it's a total
>> mystery to me)  I can't believe it's this difficult, I must
>> be doing something really dumb.
>>
>> Regards,
>>
>> CW
>>

Mime
View raw message