简体   繁体   中英

multiple POJOs/entities for the singletable

Does anyone know how I can create multiple POJOs/entities for the one table? Without getting the DuplicateEntitites error?

The reason I need multiple pojos is that I want to hide some properties in some cases.

Thank you for your time.

Updated: How I implemented Hrishikesh's suggestion.

@Entity(name="baseT1")
@Table(name="T1")
   public class BaseT1{
}

@Entity(name="T1")
@Table(name="T1")
   public class T1 extends BaseT1{
}

This gives me:

Invocation of init method failed; nested exception is     
org.hibernate.DuplicateMappingException: Duplicate table mapping T1

error.

Well, What i actually meant was something like this.

@Entity
@Table(name="T1")
public class BaseT1{

private String column1;
private String column2;

}

@Entity
@Table(name="T1")
public class BaseT2{

private String column1;

}

The classes BaseT1 and BaseT2 both map to the same T1 table but have lesser attributes defined which you will map to the actual table columns. This is one of the most simplistic way of doing it. Unless, of course if you have a discriminating value say like a record_type which distinguishes the two types of entries in a table. Then you would have to use the Discriminator solution mentioned by @jhadesdev

Java类(实体)的类名需要不同,然后您可以将同一个表映射到多个Java类。

Use Single table per class hierarchy strategy in case there is some column in the table that allow to distinguish the different cases.

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name="planetype",
    discriminatorType=DiscriminatorType.STRING
)
@DiscriminatorValue("Plane")
public class Plane { ... }

@Entity
@DiscriminatorValue("A320")
public class A320 extends Plane { ... }    

If a discriminator column does not exist or cannot be created with a stored procedure, hibernate has native support for a SQL @DiscriminatorFormula .

You could simply use @MappedSuperClass .

http://docs.jboss.org/hibernate/core/3.5/javadocs/org/hibernate/mapping/MappedSuperclass.html

@MappedSuperclass
class BaseT1 {
    // add your properties and constraints here
}


@Entity
class T1 extends BaseT1 {
}

Yes, I tried this ,it is possible to save and retrieve data into two tables using one pojo class.

One POJO Class mapped to Two Tables using Hibernate-polymorphism support

First

Create PrimaryEntityUserA.java Pojo and its hbm file 
PrimaryEntityUserA.hbm.xml  Mention  `<class 
name="PrimaryEntityUserA" 
table="user_a"
entity-name ="PrimaryEntityUserA" >`

Second

Create SecondaryEntityUserB.hbm.xml Mention `<class 
name="PrimaryEntityUserA" 
table="user_b"
entity-name ="SecondaryEntityUserB"
polymorphism = "explicit" >`

To save

PrimaryEntityUserA myObject = new PrimaryEntityUserA();
myObject.setId( new Long( 10 ) );
myObject.setName( "Tayaba" );
String entityName = "PrimaryEntityUserA";
session.save( entityName, myObject );
entityName = "SecondaryEntityUserB";
session.save( entityName, myObject );>

To Retrieve

List<PrimaryEntityUserA> usersA=LuminaHibernateUtil.getUtil().getList(session.createQuery( "from PrimaryEntityUserA a" ) );
if ( !usersA.isEmpty() ) {
    for ( PrimaryEntityUserA a : usersA ) {
        System.out.println( "Name from first table \t" + a.getName());              
   }
}
List<PrimaryEntityUserA> usersB =LuminaHibernateUtil.getUtil().getList(session.createQuery( "from SecondaryEntityUserB a" ) );
if ( !usersB.isEmpty() ) {
    for ( PrimaryEntityUserA b : usersB ) {
        System.out.println( "Name from sesond table \t" + b.getName() );                
     }
}

Conclusion : We can use 2 different tables and entity-name attribute inside both hbm files and mention same name attribute value, also mention polymorphism = "explicit" inside secondaryentity.hbm.xml.

Please find useful links for the same: http://snaike.blogspot.in/2009/04/mapping-java-object-to-two-tables-using.html

[ https://coderanch.com/t/492558/databases/Mapping-POJO-table-Hibernate-XML]

Regards
Naziya Tayaba

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM