Skip to main content

Inheritance Problem

5 replies [Last post]
dlittle
Offline
Joined: 2005-01-03

Here the scenerio:

=======================BaseObject.java====================
@MappedSuperclass
public abstract class BaseObject implements Serializable {

@Id
@Generated(strategy=GenerationType.AUTO)
@Column(name="identifier")
private Long identifier;

...

}
=======================Resource.java======================
@MappedSuperclass
public abstract class Resource extends BaseObject implements Serializable {

@Column(name="name")
private String name;

...

}
=======================Site.java==========================
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="site_type", discriminatorType=DiscriminatorType.STRING)
public abstract class Site extends Resource implements Serializable {

@Column(name="site_type")
private SiteType siteType;

public enum SiteType { DISTRIBUTION_CENTER, PLANT }

...

}
===================DistributionCenter.java================
@Entity
@DiscriminatorValue(value="DISTRIBUTION_CENTER")
public class DistributionCenter extends Site implements Serializable {

...

}

Note: Getters and Setters are omitted for brevity.

When I verify the project without DistributionCenter.java it passes all tests, however with it I get the following:

Exception Description: Multiple writable mappings exist for the field [sites.identifier]. Only one may be defined as writable, all others must be specified read-only.

and

Exception Description: Multiple writable mappings exist for the field [sites.name]. Only one may be defined as writable, all others must be specified read-only.

What am I doing wrong? I understand that toplink sees two mappings to the same database field, and to fix it I'm to add "insertable=false, updatable=false" to the @Column annotation. Does adding these values to the @Column annotation in Site.java make the field read-only in the DistributionCenter.java subclass as well. I don't think I'm doing anything that different from examples I've seen online. The abstract entity class mapped to a single table with concrete classes under it seems to be a common design. What am I missing?

Thanks,
Dusty

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ss141213
Offline
Joined: 2005-03-30

Yes, adding insertable=updatable=false to the @Column annotation in Site.java makes the field read-only in the DistributionCenter.java subclass as well.

Sahoo

dlittle
Offline
Joined: 2005-01-03

In that case I'm really confused. Where is the duplicate field mapping?

ss141213
Offline
Joined: 2005-03-30

In the absence of persistence field details for DistributionCentre class, I think I made the wrong assumption. Now I believe I know what's happenning. Since the mapped superclass' fields are inheritted by both the concrete entity subclasses, TopLink is thinking there are multiple writable mappings for the inheritted columns. Can you please file a bug with your test case?

Thanks,
Sahoo

dlittle
Offline
Joined: 2005-01-03

This makes sense as TopLink doesn't complain about the siteType field in the DistributionCenter leaf. Do I file the bug with the GlassFish or TopLink folks.

ss141213
Offline
Joined: 2005-03-30

> This makes sense as TopLink doesn't complain about
> the siteType field in the DistributionCenter leaf. Do
> I file the bug with the GlassFish or TopLink folks.

File the bug using GlassFish issue tracker at https://glassfish.dev.java.net/servlets/ProjectIssues

Use subcomponent as entity-persistence.

-- Sahoo