Reputation: 81
I have an unidirectional relation between two entities:
*@Entity
public class XXX{
@Id
private Long Id;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "YYY_ID")
private YYY yyy;
}
@Entity
public class YYY{
@Id
private Long Id;
private String someName;
}*
The entities belong to different schemas in the same database (but i may not use synonims, or give grants to schema...), so i have two persistence units:
1.persistenceUnit(xxx) - entity xxx is mapped there
2.persistenceUnit(yyy) - entity yyy is mapped there
Can i make jpa to auto perform cascade on the other persistence unit? Example:
@PersistenceContext(unitName = "xxx")
private EntityManager em;
XXX xxx = new XXX();
YYY yyy = new YYY();
yyy.setSomeName("just some name"):
xxx.setYYY(yyy);
em.persist(xxx);
This should create two objects...
Is it possible? Help appreciated. Im using JPA2, Hibernate 4 on Jboss7
Upvotes: 1
Views: 363
Reputation: 23226
The JPA specification does not mandate that mappings across multiple persistent units be supported.
You can however, depending on your database permissions, use one persistence unit and use the @Table annotation to specify the scheme/catalog for any Entities not in the default schema.
@Entity
@Table(schema="xyz")
public class XXX{
@Id
private Long Id;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "YYY_ID")
private YYY yyy;
}
Otherwise you would need to look for a solution at the database level or consider using a provider specific solution: I am not aware of anything for Hibernate but see, for example, the following EclipseLink functionality:
https://wiki.eclipse.org/EclipseLink/Examples/JPA/Composite
Starting with EclipseLink 2.3.0 JPA developers can now combine persistence units together at runtime allowing entities to be stored in different databases. This includes support for relationships between entities in different persistence units (references across databases)
Upvotes: 1