openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "B.L. Zeebub" <roger.var...@googlemail.com>
Subject Re: [NEWBIE] OpenJPA Cannot get id field returned
Date Wed, 09 Sep 2009 09:17:13 GMT



ashishpaliwal wrote:
> 
> If the .java definition is long id then mysql definition should be bigint
> id.
> But the best solution is to create the java file and let the openjpa
> create
> the table on its own. This will happen automatically when the first insert
> statement is called and openjpa finds that there is no such entity
> registered by this name.
> If it is possible then create the table and do insertion from openjpa
> itself.
> 

IMHO I don't think that it's a mismatch between the Java definitions and the
database definitions. 

Since I'm developing with the Eclipse IDE I've made a copy of my OpenJPA
project and changed the JPA provider to EclipseLink. Running the same set of
tests, I get different results. With EclipseLink, all the tests pass except
those that use EntityManager.createNativeQuery(). Since I know even less
about EclipseLink that I do about OpenJPA, I have no idea why that should
be.

So I have experimented further. You will have seen from the definition of
the Platform entity that there is a relationship with the entity PEcu. (I've
attached both .java files below) So I added the following test

@Test
	public void getPEcuByPlatformById() {
		Platform platform = (Platform) em.find(Platform.class, 1);
		Set<PEcu> set = platform.getPecus();
		assertEquals("PEcu count:",27,set.size());
	}

The test passes with EclipseLink and fails with openJPA (set is null). I've
got the Eclipse OpenJPA build time enhancer (1.2.0) installed from
http://people.apache.org/~ppoddar/eclipse and from what I can see, it does
seem to run. I am at a total loss now to know what else to check.

Regards

@Entity
@Table(name="Platform")

public class Platform implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(unique=true, nullable=false)
	private int id;

	@Column(name="PlatformName", length=32)
	private String platformName;

	//bi-directional many-to-one association to PEcu
	@OneToMany(mappedBy="platform")
	private Set<PEcu> pecus;
		 
    public Platform() {
    	
    }

	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getPlatformName() {
		return this.platformName;
	}

	public void setPlatformName(String platformName) {
		this.platformName = platformName;
	}

	public Set<PEcu> getPecus() {
		return this.pecus;
	}

	public void setPecus(Set<PEcu> pecus) {
		this.pecus = pecus;
	}
	
}

@Entity
@Table(name="PEcu")
public class PEcu implements Serializable {
	private static final long serialVersionUID = 1L;

	@EmbeddedId
	private PEcuPK id;

	@Column(nullable=false, length=1)
	private String fitment;

	@Column(nullable=false, length=64)
	private String idsName;

	@Column(nullable=false, length=128)
	private String idsText;

	@Column(nullable=false, length=3)
	private String rxAddress;

	//bi-directional many-to-one association to Platform
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="platformId", nullable=false, insertable=false,
updatable=false)
	private Platform platform;

	//bi-directional many-to-one association to Assembly
	@OneToMany(mappedBy="pecu")
	private Set<Assembly> assemblies;

	//bi-directional many-to-one association to PEcuAcronym
	@OneToMany(mappedBy="pecu")
	private Set<PEcuAcronym> pecuAcronyms;

	//uni-directional one-to-one association to PEcuText
	@OneToOne(fetch=FetchType.LAZY)
	@JoinColumns({
		@JoinColumn(name="platformId", referencedColumnName="platformId",
nullable=false, insertable=false, updatable=false),
		@JoinColumn(name="busid", referencedColumnName="busId", nullable=false,
insertable=false, updatable=false),
		@JoinColumn(name="txaddress", referencedColumnName="txAddress",
nullable=false, insertable=false, updatable=false),
		})
	private PEcuText pecuText;

    public PEcu() {
    }

	public PEcuPK getId() {
		return this.id;
	}

	public void setId(PEcuPK id) {
		this.id = id;
	}
	
	public String getFitment() {
		return this.fitment;
	}

	public void setFitment(String fitment) {
		this.fitment = fitment;
	}

	public String getIdsName() {
		return this.idsName;
	}

	public void setIdsName(String idsName) {
		this.idsName = idsName;
	}

	public String getIdsText() {
		return this.idsText;
	}

	public void setIdsText(String idsText) {
		this.idsText = idsText;
	}

	public String getRxAddress() {
		return this.rxAddress;
	}

	public void setRxAddress(String rxAddress) {
		this.rxAddress = rxAddress;
	}

	public Platform getPlatform() {
		return this.platform;
	}

	public void setPlatform(Platform platform) {
		this.platform = platform;
	}
	
	public Set<Assembly> getAssemblies() {
		return this.assemblies;
	}

	public void setAssemblies(Set<Assembly> assemblies) {
		this.assemblies = assemblies;
	}
	
	public Set<PEcuAcronym> getPecuAcronyms() {
		return this.pecuAcronyms;
	}

	public void setPecuAcronyms(Set<PEcuAcronym> pecuAcronyms) {
		this.pecuAcronyms = pecuAcronyms;
	}
	
	public PEcuText getPecuText() {
		return this.pecuText;
	}

	public void setPecuText(PEcuText pecuText) {
		this.pecuText = pecuText;
	}
	
}

@Embeddable
public class PEcuPK implements Serializable {
	//default serial version id, required for serializable classes.
	private static final long serialVersionUID = 1L;

	@Column(unique=true, nullable=false)
	private int platformId;

	@Column(unique=true, nullable=false)
	private int busid;

	@Column(unique=true, nullable=false, length=3)
	private String txaddress;

    public PEcuPK() {
    }
	public int getPlatformId() {
		return this.platformId;
	}
	public void setPlatformId(int platformId) {
		this.platformId = platformId;
	}
	public int getBusid() {
		return this.busid;
	}
	public void setBusid(int busid) {
		this.busid = busid;
	}
	public String getTxaddress() {
		return this.txaddress;
	}
	public void setTxaddress(String txaddress) {
		this.txaddress = txaddress;
	}

	public boolean equals(Object other) {
		if (this == other) {
			return true;
		}
		if (!(other instanceof PEcuPK)) {
			return false;
		}
		PEcuPK castOther = (PEcuPK)other;
		return 
			(this.platformId == castOther.platformId)
			&& (this.busid == castOther.busid)
			&& this.txaddress.equals(castOther.txaddress);

    }
    
	public int hashCode() {
		final int prime = 31;
		int hash = 17;
		hash = hash * prime + this.platformId;
		hash = hash * prime + this.busid;
		hash = hash * prime + this.txaddress.hashCode();
		
		return hash;
    }
}

-- 
View this message in context: http://n2.nabble.com/NEWBIE-OpenJPA-Cannot-get-id-field-returned-tp3601995p3609429.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Mime
View raw message