openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jc222 <jc...@free.fr>
Subject Transient field with @ManagedInterface
Date Tue, 16 Dec 2008 22:31:12 GMT

Hi all,
Always my pb of managed interface and transient field.
I really don't know how to do the job ...
Can anybody help me ?

Here is the full example (first the interfaces, then the implementation
classes , the main test class, the presistence.xml and the exception.....

---------------------------------------------------------
Param.java:

package tt;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import org.apache.openjpa.persistence.ManagedInterface;

@ManagedInterface
@Embeddable
public interface Param {

	void setValue(int value);

	@Column(name = "value")
	int getValue();
}

--------------------------------------------------------
Base.java:

package tt;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

import org.apache.openjpa.persistence.ManagedInterface;

@ManagedInterface
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public interface Base {
	
	void setId(long id);

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	long getId();
}
---------------------------------------------------------------
Thing.java

package tt;

import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.openjpa.persistence.ManagedInterface;

@ManagedInterface
@Entity
@Table(name = "if")
public interface Thing extends Base {

	void setName(String name);

	String getName();

	void setParam(Param param);

	@Embedded
	@AttributeOverride(name = "value", column = @Column(name = "param1"))
	Param getParam();

	@Transient
	int getFoo();

	void setFoo(int foo);
}
----------------------------------------------
ParamImpl .java:

package ttimpl;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import tt.Param;

@Embeddable
public class ParamImpl implements Param {

	protected int value;

	@Override
	@Column(name = "value")
	public int getValue() {
		return value;
	}

	@Override
	public void setValue(int value) {
		this.value = value;
	}

}
-------------------------------------------------
BaseImpl.java:

package ttimpl;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import tt.Base;

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class BaseImpl implements Base {

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	protected long id;

	@Override
	public long getId() {
		return id;
	}

	@Override
	public void setId(long id) {
		this.id = id;
	}

}
-----------------------------------------
ThingImpl.java

package ttimpl;

import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Table;
import tt.Param;
import tt.Thing;

@Entity
@Table(name = "thing")
public class ThingImpl extends BaseImpl implements Thing {

	protected String name;

	@Embedded
	@AttributeOverride(name = "value", column = @Column(name = "param1"))
	protected Param param;

	protected int foo;

	@Override
	public String getName() {
		return name;
	}

	@Override
	public Param getParam() {
		return param;
	}

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

	@Override
	public void setParam(Param param) {
		this.param = param;
	}

	@Override
	public int getFoo() {
		return foo;
	}

	@Override
	public void setFoo(int foo) {
		this.foo = foo;
	}

}

----------------------------------------------
Main.java:

package ttimpl;

import java.util.logging.Logger;
import javax.persistence.Query;
import org.apache.openjpa.persistence.OpenJPAEntityManager;
import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
import org.apache.openjpa.persistence.OpenJPAPersistence;
import tt.Param;
import tt.Thing;

public class Main {
	/**
	 * logger de la classe
	 */
	private static Logger _logger = Logger.getLogger("tt");

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		_logger.info("begin");
		 EntityManagerFactory factory =
Persistence.createEntityManagerFactory("tt", System.getProperties());
		 EntityManager em = factory.createEntityManager();


		 em.getTransaction().begin();
		        
		 Thing thing = new ThingImpl();
		 thing.setName("thing");
		 thing.setParam(new ParamImpl());
		 thing.getParam().setValue(12);
		 thing.setFoo(45);
		        
		 em.persist(thing);
		        
		 em.getTransaction().commit();
		 em.close();
		_logger.info("end");
	}

}
-------------------------------------------------------------
Persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
	<persistence-unit name="tt" transaction-type="RESOURCE_LOCAL">
		<provider>
			org.apache.openjpa.persistence.PersistenceProviderImpl
        </provider>
		<mapping-file>reversemapping/orm.xml</mapping-file>
		<class>tt.Thing</class>
		<class>tt.Param</class>
		<class>tt.Base</class>
		<class>ttimpl.ThingImpl</class>
		<class>ttimpl.ParamImpl</class>
		<class>ttimpl.BaseImpl</class>
		<properties>
			<property name="openjpa.ConnectionURL"
value="jdbc:derby:openjpa-database;create=true" />
			<property name="openjpa.ConnectionDriverName"
value="org.apache.derby.jdbc.EmbeddedDriver" />
			<property name="openjpa.ConnectionUserName" value="user" />
			<property name="openjpa.ConnectionPassword" value="secret" />
			<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" />
		</properties>
	</persistence-unit>
</persistence>
----------------------------------------------------------------------

StrackTrace:

16 déc. 2008 22:44:18 ttimpl.Main main
INFO: begin
55  tt  INFO   [main] openjpa.Runtime - Starting OpenJPA 1.2.0
148  tt  INFO   [main] openjpa.jdbc.JDBC - Using dictionary class
"org.apache.openjpa.jdbc.sql.DerbyDictionary".
1605  tt  INFO   [main] openjpa.Enhance - Creating subclass for "[class
ttimpl.ParamImpl, class ttimpl.ThingImpl, class ttimpl.BaseImpl]". This
means that your application will be less efficient and will consume more
memory than it would if you ran the OpenJPA enhancer. Additionally, lazy
loading will not be available for one-to-one and many-to-one persistent
attributes in types using field access; they will be loaded eagerly instead.
Exception in thread "main" <openjpa-1.2.0-r422266:683325 fatal store error>
org.apache.openjpa.persistence.RollbackException:
org.apache.openjpa.enhance.ReflectingPersistenceCapable cannot be cast to
tt.Param$21565531openjpaimpl
	at
org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:523)
	at ttimpl.Main.main(Main.java:66)
Caused by: <openjpa-1.2.0-r422266:683325 nonfatal general error>
org.apache.openjpa.persistence.PersistenceException:
org.apache.openjpa.enhance.ReflectingPersistenceCapable cannot be cast to
tt.Param$21565531openjpaimpl
	at org.apache.openjpa.kernel.BrokerImpl.embed(BrokerImpl.java:2703)
	at
org.apache.openjpa.kernel.SingleFieldManager.embed(SingleFieldManager.java:846)
	at
org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager.java:588)
	at
org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager.java:562)
	at
org.apache.openjpa.kernel.SingleFieldManager.preFlush(SingleFieldManager.java:478)
	at
org.apache.openjpa.kernel.StateManagerImpl.preFlush(StateManagerImpl.java:2828)
	at org.apache.openjpa.kernel.PNewState.beforeFlush(PNewState.java:39)
	at
org.apache.openjpa.kernel.StateManagerImpl.beforeFlush(StateManagerImpl.java:959)
	at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:1948)
	at org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:1908)
	at
org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(BrokerImpl.java:1826)
	at
org.apache.openjpa.kernel.LocalManagedRuntime.commit(LocalManagedRuntime.java:81)
	at org.apache.openjpa.kernel.BrokerImpl.commit(BrokerImpl.java:1350)
	at
org.apache.openjpa.kernel.DelegatingBroker.commit(DelegatingBroker.java:877)
	at
org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:512)
	... 1 more
Caused by: java.lang.ClassCastException:
org.apache.openjpa.enhance.ReflectingPersistenceCapable cannot be cast to
tt.Param$21565531openjpaimpl
	at tt.Param$21565531openjpaimpl.pcCopyFields(Unknown Source)
	at org.apache.openjpa.kernel.BrokerImpl.embed(BrokerImpl.java:2681)
	... 15 more

-- 
View this message in context: http://n2.nabble.com/Transient-field-with-%40ManagedInterface-tp1665281p1665281.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Mime
View raw message