...">

Is it possible to use a foreign key without binding an object to an object?

Assuming the following mappings:

<class name="A" table="a_table">
  <id name="id"/>
  <many-to-one name="entityB" column="fk_B" not-null="false" unique="true"/>
</class>

<class name="B" table="b_table">
  <id name="id"/>
</class>

Java class:

public class A {
   private long id;
   private B entityB;
   // getters and setters skipped
}

Is it possible to change the display of Hibernate so that the foreign key is still enforced and created by Hibernate at startup , but the class Awill look like this:

public class A {
   private long id;
   private long idOfB;
   // getters and setters skipped
}

I understand that if I converted <many-to-one...to <property..., it would work, but the foreign key would not be involved in the database.

I need to do this because the object Bmay (or may not) be initialized separately, which sometimes raises org.hibernate.LazyInitializationException: could not initialize proxy - no Sessionexceptions that occur when called a.getB(). I would prefer to use it as long idOfBwell as load the entire object when necessary; it will also speed up the loading of the object A.

, , ( ) , a.getB().getId(), LazyInitializationException, a.getIdOfB(), .

.

+5
4

, < many-to-one... < property..., , , .

, :

public class EntityA {

    private Integer idOfB;

    private EntityB entityB;

    // getter and setter's

}

<class name="A" table="a_table">
    <id name="id"/>
    <property name="idOfB" column="fk_B" not-null="false" unique="true"/>
    <many-to-one name="entityB" update="false" insert="false" column="fk_B"/>
</class>

, , , . Hibernate . , update = "false" insert = "false" entityB.

,

+7

DDL hibm.xml hibernate:

<hibernate-mapping>
    ...
    <database-object>
        <create>[CREATE FK]</create>
        <drop>[DROP FK]</drop>
    </database-object> 
</hibernate-mapping>

, .

5.7.

+4

, , - FK B-, A. JPA, , .

@Entity
public class B
{
    private long id;
    private List<A> aList;

    @Id
    @Column( name = "ID" )
    public long getId()
    {
        return id;
    }

    @OneToMany
    @JoinColumn( name = "B_ID" )
    public List<A> getAList()
    {
        return aList;
    }
    public void setId( long id )
    {
        this.id = id;
    }
    public void setAList( List<A> aList )
    {
        this.aList = aList;
    }        
}

A.java :

@Entity
public class A
{
    private long id;
    private long idOfB;

    @Id
    @Column( name = "ID" )
    public long getId()
    {
        return id;
    }
    @Column( name = "B_ID" )
    public long getIdOfB()
    {
        return idOfB;
    }
    public void setId( long id )
    {
        this.id = id;
    }
    public void setIdOfB( long idOfB )
    {
        this.idOfB = idOfB;
    }   
}
0

, Hibernate. , - , . , Hibernate , . , , : .

If you use spring, you can use OpenEntityManagerInViewFilter so that the session is kept open until the view is displayed.

0
source

All Articles