JBoss EAP 6
Hibernate 4
I have a J2EE application with a web browser client. (Apache click) Both internal business logic and the client use the same object objects.
I would like to have all the relationships in objects set to lazy loading. So I have good performance.
But when using entities on the client (this is server-side code for the Apache click) I need many relationships to be loaded. Client code accesses the server through a bean session.
So, I have several ways to solve this problem:
Create 2 from each JPA object, one with eager loading and one with lazy loading. And then use one that eagerly loads in the client, and one that has lazy loading on the server. Most of the server logic will be in a transaction, so lazy loading is great here.
Make all relationships lazy loading. When accessing objects from the client, make sure that there is a transaction. (@TransactionAttribute (TransactionAttributeType.REQUIRED)) and encode access to the required fields so that they are available after the bean session. But this means that I have to start the transaction when it is not required, i.e. If I get only some objects. And I have to support more code. And I must know exactly what kind of relationship the client needs.
, -, 2 , - lazy, , . :
@MappedSuperclass
public class SuperOrder {
@Id
@Column(name = "id")
@GeneratedValue(.....)
private Long id;
@Column(name = "invoice", length = 100)
private String invoice;
1
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "testorder")
@SequenceGenerator(....)
public class Order extends SuperOrder {
@ManyToOne(targetEntity = PrintCustomerEnt.class, fetch = FetchType.EAGER, optional = true)
@JoinColumn(name = "print_customer_id", nullable = true)
@ForeignKey(name = "fk_print_customer")
@Valid
private PrintCustomerEnt printCustomer;
public PrintCustomerEnt getPrintCustomer() {
return printCustomer;
}
public void setPrintCustomer(final PrintCustomerEnt printCustomer) {
this.printCustomer = printCustomer;
}
}
2
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "testorder")
@SequenceGenerator(...)
public class LazyOrder extends SuperOrder {
@Transient
private String printCustomerName;
@Column(name = "print_customer_id", nullable = true)
private Long printCustomerId;
... - .
, . , .