openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Christian Defoy" <christian.de...@gmail.com>
Subject Re: Embedded entities are deleted last
Date Fri, 06 Jul 2007 02:50:22 GMT
Hi Markus,

Here is the code to reproduce the problem.  I include the source code,
my persistence.xml file and the schema script for my database.  As I
mentionned in my first post, I am using Hypersonic (in case it is
important).

Shape.java:
----
import javax.persistence.*;

@Entity
@Table(name="SHAPE")
@SecondaryTable(name="CIRCLE",
                pkJoinColumns=@PrimaryKeyJoinColumn(name="shape_id",
referencedColumnName="shape_id"))
public class Shape
{
    @Column(name="shape_id")
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private int type;

    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name="x",column=@Column(name="x",table="CIRCLE")),
        @AttributeOverride(name="y",column=@Column(name="y",table="CIRCLE")),
        @AttributeOverride(name="radius",column=@Column(name="radius",table="CIRCLE"))})
    private Circle circle;

    public int getId() { return id; }
    public void setType( int aType ) { type = aType; }
    public void setCircle( Circle anCircle ) { circle = anCircle; }
}

Circle.java
----
import javax.persistence.Embeddable;

@Embeddable
public class Circle
{
    private int x;
    private int y;
    private int radius;
    public int getRadius() { return radius; }
    public void setRadius( int anRadius ) { radius = anRadius; }
    public int getX() { return x; }
    public void setX( int anX ) { x = anX; }
    public int getY() { return y;  }
    public void setY( int anY ) { y = anY; }
}

ShapeHelper.java
----
import java.util.HashMap;
import java.util.Map;

import javax.persistence.*;

public class ShapeHelper
{
    public static void main( String[] args ) throws Exception
    {
        Map<String,String> params = new HashMap<String,String>();
        params.put( "openjpa.Id", "basic" );
        params.put( "openjpa.ConnectionUserName", "sa" );
        params.put( "openjpa.ConnectionPassword", "" );
        params.put( "openjpa.ConnectionURL",
"jdbc:hsqldb:hsql://localhost/jpa-db" );
        params.put( "openjpa.ConnectionDriverName", "org.hsqldb.jdbcDriver" );

        EntityManagerFactory factory =
Persistence.createEntityManagerFactory( "basic", params );

        EntityManager manager = factory.createEntityManager();

        Shape base = new Shape();
        base.setType( 0 );

        // Set embedded data in the shape
        Circle circle = new Circle();
        circle.setRadius( 10 );
        circle.setX( 100 );
        circle.setY( 50 );
        base.setCircle( circle );

        EntityTransaction transaction = manager.getTransaction();
        transaction.begin();
        manager.persist( base );
        transaction.commit();
        manager.close();

        // Load the thing
        manager = factory.createEntityManager();
        Shape loadedBase = manager.find( Shape.class, base.getId() );

        transaction = manager.getTransaction();
        transaction.begin();
        manager.remove( loadedBase );
        transaction.commit();
        manager.close();


        // Clean up the factory
        factory.close();
    }
}

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="basic" transaction-type="RESOURCE_LOCAL">
    	<class>Shape</class>
    	<class>Circle</class>

	    <properties>
		    <property name="openjpa.Log" value="DefaultLevel=WARN,
Runtime=WARN, Tool=WARN, SQL=TRACE"/>
		    <property name="openjpa.ConnectionRetainMode" value="always"/>
		</properties>
    </persistence-unit>


</persistence>

schema:
----
DROP TABLE SHAPE IF EXISTS;
CREATE CACHED TABLE SHAPE
(
	SHAPE_ID IDENTITY NOT NULL PRIMARY KEY,
	TYPE INTEGER NOT NULL
);

DROP TABLE CIRCLE IF EXISTS;
CREATE CACHED TABLE CIRCLE
(
	CIRCLE_SHAPE_ID INTEGER NOT NULL PRIMARY KEY,
	X INTEGER NOT NULL,
	Y INTEGER NOT NULL,
	RADIUS INTEGER NOT NULL
);

ALTER TABLE CIRCLE ADD CONSTRAINT F_CIRCLE_1 FOREIGN KEY
(CIRCLE_SHAPE_ID) REFERENCES SHAPE(SHAPE_ID);

Hope you'll be able to reproduce it :)

Christian

Mime
View raw message