JAVA: JPA – Zitate

ORM and JPA

To use Hibernate effectively, you must be able to view and interpret the SQL statements it issues and understand their performance implications.

Testen auf Neuanlage (S.70)

id.getID==null => Objekt ist neu angelegt

If you want to test whether an item is new, a null check is probably easier to understand for someone else reading your code.

JPA-Strategien (S.72)

If you require consistent portable behavior, and identifier values to be available before INSERT s, use enhanced-sequence ,

In general, we prefer pre-insert generation strategies that produce identifier values independently, before INSERT.

To summarize, our recommendations on identifier generator strategies are as follows:

  • In general, we prefer pre-insert generation strategies that produce identifier values independently before INSERT .
  • Use enhanced-sequence , which uses a native database sequence when sup-ported and otherwise falls back to an extra database table with a single column and row, emulating a sequence.

Entity-Annotation (S.77)

Hibernate 5 knows the reserved keywords of your DBMS through the configured database dialect. Hibernate 5 can automatically put quotes around such strings when generating SQL . You can enable this automatic quoting with hibernate.auto_quote keyword=true in your persistence unit configuration (sonst in JPA: Table(name = „\“USER\““))

Hibernate ships with several strategies to implement legacy- or JPA -compliant default names. The default strategy is Implicit-NamingStrategyJpaCompliantImpl . The overridden method toPhysicalTableName() prepends CE_ to all generated table
names in your schema.

With @javax.persistence.Entity(name = „AuctionItem“) you resolve the naming conflict with another Item class in another package.

By default, Hibernate creates SQL statements for each persistent class when the persistence unit is created, on startup. you can disable this startup SQL generation and switch to dynamic state-
ments generated at runtime for insert and update

  • (@org.hibernate.annotations.DynamicInsert
  • @org.hibernate.annotations.DynamicUpdate.

@Entity @org.hibernate.annotations.Immutable makes classes immutabel. A POJO is immutable if no public setter methods for any properties of the class are exposed—all values are set in the  constructor.

Embedded Values (S.81)

We recommend the Bean Validation @NotNull annotation (statt @column(nullable = false) oder @Basic(optional=false))

Annotations are never on the setter methods.

The @Access(AccessType.PROPERTY) setting on the field switches this particular property to runtime access through getter/setter methods by the JPA provider. If the default (or explicit) access type of the entity would be through property getter and setter methods, @Access(AccessType.FIELD) on a getter method would tell Hibernate to access the field directly.

The noop property accessor: It might be a legacy column or a column maintained by another application or database trigger.

Berechnete Felder

@org.hibernate.annotations.Formula( „(select avg(b.AMOUNT) from BID b where b.ITEM_ID = ID)“)
protected BigDecimal averageBidAmount;

@Column(name = „IMPERIALWEIGHT“) @org.hibernate.annotations.ColumnTransformer(
read = „IMPERIALWEIGHT / 2.20462“, write = „? * 2.20462“)
protected double metricWeight;

Default Values (S.88)

@Temporal(TemporalType.TIMESTAMP)
@Column(insertable = false, updatable = false)
@org.hibernate.annotations.Generated(
org.hibernate.annotations.GenerationTime.ALWAYS
)
protected Date lastModified;
@Column(insertable = false)
@org.hibernate.annotations.ColumnDefault(„1.00“)
@org.hibernate.annotations.Generated(
org.hibernate.annotations.GenerationTime.INSERT
)
protected BigDecimal initialPrice;

Schreibe einen Kommentar