-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: one table mapped as interface -persisted as impl need sample
PostPosted: Tue Feb 24, 2004 2:03 am 
Newbie

Joined: Sat Feb 21, 2004 3:02 pm
Posts: 14
The documentation says

It is perfectly acceptable for the named persistent class to be an interface. You would then declare implementing classes of that interface using the <subclass> element.
com.microsoft.northwind.Product is an interface
com.microsoft.northwind.impl.ProductImpl is the only implementing class

So I try
<class name="com.microsoft.northwind.Product" table="Products" polymorphism="explicit" >
<id name="ProductID" type="int" unsaved-value="null" >
<column name="ProductID" sql-type="int identity" not-null="true" />
<generator class="hilo" />
</id>
<property name="productName" column="ProductName" type="string" not-null="true" />
<property name="quantityPerUnit" column="QuantityPerUnit" type="string" not-null="true" />
<subclass name="com.microsoft.northwind.impl.ProductImpl" />
</class>
NO Luck a discriminator is required
Using productName - a required column as value="non null"
means productName is not read
I have not figured out how to use ProductID value="non null"
as a discriminator
If I declare the class name as ProductImpl hibernate will not handle my properties which are declared as the interface Product
How do I do this???

_________________
Steven M. Lewis PhD
4221 105th Ave NE
Kirkland, WA 98033
425-889-2694
206-384-1340 (cell)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 2:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No! In this case there is no reason to declare the interface at all.


Top
 Profile  
 
 Post subject: Interface vs implementation still unclear
PostPosted: Tue Feb 24, 2004 12:16 pm 
Newbie

Joined: Sat Feb 21, 2004 3:02 pm
Posts: 14
OK - so When I have only one implementation I can map the implementation not the interface.
Ther problem comes with associations. I want the Procuct interface to
declare that the return from the one-to-many association to Subblier is the
supplier interface not the implementation. This is reasonable - the interfaces should not need to reference implementations.
The problem is that if I do not map the interface Supplier I get the error listed below - Supplier is unmapped. If I map the interface (as I think is proper) then I need a way to tell the persistor to ALWAYS instantiate the implementation. I suppose a custom persistor could do this but do not believe this is needed - again one good sample would make the issue very clear

<hibernate-mapping>
<class name="com.microsoft.northwind.impl.ProductImpl" table="Products" >
<id name="ProductID" type="int" unsaved-value="null" >
<column name="ProductID" sql-type="int identity" not-null="true" />
<generator class="hilo" />
</id>
<property name="productName" column="ProductName" type="string" not-null="true" />
<many-to-one name="Supplier" column="SupplierID" not-null="false" />
...
</class>
<class name="com.microsoft.northwind.impl.SupplierImpl" table="Suppliers" >
<id name="SupplierID" type="int" unsaved-value="null" >
<column name="SupplierID" sql-type="int identity" not-null="true" />
<generator class="hilo" />
</id>
...
<set name="Products" table="Products" inverse="true" cascade="all" >
<key column="SupplierID" />
<one-to-many class="com.microsoft.northwind.impl.SupplierImpl" />
</set>
</class>
</hibernate-mapping>

public interface Product ...
{
public long getProductID();
...
public com.microsoft.northwind.Supplier getSupplier();
public void setSupplier(com.microsoft.northwind.Supplier in);
}


public interface Supplier ...
{
public long getSupplierID();

public Set getProducts();
}


net.sf.hibernate.MappingException: An association from the table Products refers to an unmapped class: com.microsoft.northwind.Supplier
at net.sf.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:643)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:743)
at com.babel17.northwind.HibernateTest.createSessionFactory(HibernateTest.java:53)
at com.babel17.northwind.HibernateTest.testProduct(HibernateTest.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

_________________
Steven M. Lewis PhD
4221 105th Ave NE
Kirkland, WA 98033
425-889-2694
206-384-1340 (cell)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 12:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is why we have metadata. Obviously if the Java property had to be exactly the same as the type declared in the metadata, we could determine everything using reflection!

Just tell Hibernate what the concrete type is, and declare whatever interface type you like in your JavaBean. Easy.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 9:25 pm 
Newbie

Joined: Sat Feb 21, 2004 3:02 pm
Posts: 14
"Just tell Hibernate what the concrete type is, and declare whatever interface type you like in your JavaBean. Easy."

That is EXACTLY what I am doing in the sample above -
I get an error because the interface is not mapped - the interface is the property (as a foreign key an association) but the mapping declares the impl file and I get the error

net.sf.hibernate.MappingException: An association from the table Products refers to an unmapped class: com.microsoft.northwind.Supplier

Supplier is the interface SupplierImpl is declared in the map and it does not work

One good piece of sample code would help alot and I promise to pose a sample if and when I get this working

_________________
Steven M. Lewis PhD
4221 105th Ave NE
Kirkland, WA 98033
425-889-2694
206-384-1340 (cell)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.