You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by li...@terra.com.br on 2010/06/14 20:43:20 UTC

Mapping more than one @ManyToOne relation doesn’t work

Hi,

I’m trying to build my first application using OpenJPA, but I got stuck with an odd problem. 

When declare more than one @ManyToOne relation on one entity, only one (sometimes none) of them is mapped correctly on the database. I’ve tried with existing tables and creating the tables by 
running the program, but the behavior doesn’t change.

Here is the example I’m trying:

@Entity
public class Project implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	private long id;
	
	@Column(nullable = false, length=200)
	private String name;
...
}

@Entity
public class Developer implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3366659313976911445L;

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	protected long id;
	
	@Column(nullable = false, length = 200)
	private String name;
...
}

@Entity
public class Participation implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5739921124942905231L;

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

	@ManyToOne(cascade = CascadeType.ALL)
	@ForeignKey
	private Project project;

	@ManyToOne(cascade = CascadeType.ALL)
	@ForeignKey
	private Developer developer;
...
}


In Participation either a column referring to Project or one referring to Developer is created. If I add another @ManyToOne relation, I still get only the mapping of one column correctly.

I’m using PosgreSQL.

Any ideas of what is happening? 
Help me, please.

Lile



Re: Mapping more than one @ManyToOne relation doesn’t work

Posted by Lile Hattori <li...@terra.com.br>.
Hi,

Here is the 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="2.0">
	<persistence-unit name="project">
		
		<description>project</description>
		<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
		
		<class>ch.usi.syde.server.persistence.db.dao.Project</class>
		<class>ch.usi.syde.server.persistence.db.dao.Developer</class>
		<class>ch.usi.syde.server.persistence.db.dao.Participation</class>

		<properties>
			<property name="openjpa.jdbc.DBDictionary" value="postgres" />
			<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" />
			<property name="openjpa.ConnectionDriverName"
value="org.postgresql.Driver" />
			<property name="openjpa.ConnectionURL"
value="jdbc:postgresql://localhost:5432/project" />
			<property name="openjpa.ConnectionUserName" value="[user]" />
			<property name="openjpa.ConnectionPassword" value="[psswd]" />
			<property name="openjpa.Log" value="SQL=TRACE" />
		</properties>

	</persistence-unit>
</persistence>


Here is the output of the java -jar /path/to/openjpa.jar:

Apache svn revision: 422266:935683

os.name: Mac OS X
os.version: 10.6.3
os.arch: x86_64

java.version: 1.6.0_20
java.vendor: Apple Inc.

java.class.path:
	openjpa-all-2.0.0.jar


And here is the SQL trace I get:

7775  project  TRACE  [main] openjpa.jdbc.SQL - <t 1902059420, conn
1129698211> executing stmnt 630883350 CREATE TABLE Developer (id
BIGINT NOT NULL, connected BOOL NOT NULL, name VARCHAR(200) NOT NULL,
version ABSTIME, PRIMARY KEY (id))
7884  project  TRACE  [main] openjpa.jdbc.SQL - <t 1902059420, conn
1129698211> [109 ms] spent
8175  project  TRACE  [main] openjpa.jdbc.SQL - <t 1902059420, conn
165054145> executing stmnt 2112994712 CREATE TABLE
OPENJPA_SEQUENCE_TABLE (ID SMALLINT NOT NULL, SEQUENCE_VALUE BIGINT,
PRIMARY KEY (ID))
8254  project  TRACE  [main] openjpa.jdbc.SQL - <t 1902059420, conn
165054145> [79 ms] spent
8560  project  TRACE  [main] openjpa.jdbc.SQL - <t 1902059420, conn
885516455> executing stmnt 337501626 CREATE TABLE Participation (id
BIGINT NOT NULL, version ABSTIME, PROJECT_ID BIGINT, PRIMARY KEY (id))
8707  project  TRACE  [main] openjpa.jdbc.SQL - <t 1902059420, conn
885516455> [147 ms] spent
9025  project  TRACE  [main] openjpa.jdbc.SQL - <t 1902059420, conn
621221153> executing stmnt 1836265170 CREATE TABLE Project (id BIGINT
NOT NULL, name VARCHAR(200) NOT NULL, version ABSTIME, PRIMARY KEY
(id))
9117  project  TRACE  [main] openjpa.jdbc.SQL - <t 1902059420, conn
621221153> [92 ms] spent
9492  project  TRACE  [main] openjpa.jdbc.SQL - <t 1902059420, conn
1002735346> executing stmnt 887699865 ALTER TABLE Participation ADD
FOREIGN KEY (PROJECT_ID) REFERENCES Project (id) DEFERRABLE
9581  project  TRACE  [main] openjpa.jdbc.SQL - <t 1902059420, conn
1002735346> [88 ms] spent


As you can see, it doesn't create a column for DEVELOPER_ID.

Any help will be greatly appreciated.

Lile

On Mon, Jun 14, 2010 at 10:30 PM, Pinaki Poddar <pp...@apache.org> wrote:
>
> Hi,
> OpenJPA 2.0.x defines the following schema on Postgres as per the domain
> model posted:
>
> CREATE TABLE Developer (id BIGINT NOT NULL, name VARCHAR(255), PRIMARY KEY
> (id))
> CREATE TABLE Participation (id BIGINT NOT NULL, DEVELOPER_ID BIGINT,
> PROJECT_ID BIGINT, PRIMARY KEY (id))
> CREATE TABLE Project (id BIGINT NOT NULL, name VARCHAR(255), PRIMARY KEY
> (id))
> ALTER TABLE Participation ADD FOREIGN KEY (DEVELOPER_ID) REFERENCES
> Developer (id) DEFERRABLE
> ALTER TABLE Participation ADD FOREIGN KEY (PROJECT_ID) REFERENCES Project
> (id) DEFERRABLE
>
> 1. Please post
>    a) persistence.xml for further analysis
>    b) output of $ java -jar /path/to/openjpa.jar
>
>   for further analysis
>
> -----
> Pinaki
> --
> View this message in context: http://openjpa.208410.n2.nabble.com/Mapping-more-than-one-ManyToOne-relation-doesn-t-work-tp5178742p5179201.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>

Re: Mapping more than one @ManyToOne relation doesn’t work

Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
OpenJPA 2.0.x defines the following schema on Postgres as per the domain
model posted:

CREATE TABLE Developer (id BIGINT NOT NULL, name VARCHAR(255), PRIMARY KEY
(id))
CREATE TABLE Participation (id BIGINT NOT NULL, DEVELOPER_ID BIGINT,
PROJECT_ID BIGINT, PRIMARY KEY (id))
CREATE TABLE Project (id BIGINT NOT NULL, name VARCHAR(255), PRIMARY KEY
(id))
ALTER TABLE Participation ADD FOREIGN KEY (DEVELOPER_ID) REFERENCES
Developer (id) DEFERRABLE
ALTER TABLE Participation ADD FOREIGN KEY (PROJECT_ID) REFERENCES Project
(id) DEFERRABLE

1. Please post 
    a) persistence.xml for further analysis
    b) output of $ java -jar /path/to/openjpa.jar

   for further analysis

-----
Pinaki 
-- 
View this message in context: http://openjpa.208410.n2.nabble.com/Mapping-more-than-one-ManyToOne-relation-doesn-t-work-tp5178742p5179201.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.