Enterprise Java Development@TOPIC@
insertable - column will be included in SQL during initial insert
updatable - column will be included in SQL during follow-on updates
precision * - defines number of digits used for Numeric types (e.g., 100.02 is precision=5)
scale * - defines number of digits to use to the right of decimal place (e.g., 100.01 is scale=2)
Figure 47.2. More on Precision
//update beyond the precision values -- too many digits overall
car2 = car3;
car2.setCost(new BigDecimal("123456.66"));
try {
em.flush();
fail("database accepted too many digits");
} catch (PersistenceException ex) {
log.info("caught expected exception:" + ex);
}
-caught expected exception:javax.persistence.PersistenceException: org.hibernate.exception.DataException: could not execute statement
Figure 47.4. Transient Example orm.xml
<entity class="ejava.examples.orm.core.mapped.Tank" access="PROPERTY">
<table name="ORMCORE_TANK"/>
<attributes>
<id name="id"/>
<transient name="makeModel"/>
</attributes>
</entity>
Figure 47.5. Transient Example Database Schema
create table ORMCORE_TANK ( id bigint not null, make varchar(255), model varchar(255), primary key (id) )
Figure 47.6. Transient Example Test
ejava.examples.orm.core.annotated.Tank tank = new Tank(1);
tank.setMake("acme");
tank.setModel("great guns");
//insert a row in the database
em.persist(tank);
log.info("created tank:" + tank);
-created tank:ejava.examples.orm.core.annotated.Tank@4eef4eb7make=acme, model=great guns
Figure 47.8. CLOB Example Entity Class
@Entity
@Table(name="ORMCORE_UMBRELLA")
public class Umbrella {
@Lob
@Basic(fetch=FetchType.LAZY) //ignored
public char[] getMake() {
return make.toCharArray();
}
public void setMake(char[] make) {
this.make = new String(make);
}
Figure 47.10. Temporal/Enum Example Entity Class
@Entity
@Table(name="ORMCORE_VASE")
public class Vase {
@Id
private long id;
@Temporal(TemporalType.DATE)
private Date aDate;
@Temporal(TemporalType.TIME)
private Date aTime;
@Temporal(TemporalType.TIMESTAMP)
private Date aTimestamp;
@Enumerated(EnumType.ORDINAL)
private ColorType colorId;
@Enumerated(EnumType.STRING)
private ColorType colorName;
Figure 47.11. Temporal/Enum Example orm.xml
<entity class="ejava.examples.orm.core.mapped.Vase" access="FIELD">
<table name="ORMCORE_VASE"/>
<attributes>
<id name="id"/>
<basic name="aDate">
<temporal>DATE</temporal>
</basic>
<basic name="aTime">
<temporal>TIME</temporal>
</basic>
<basic name="aTimestamp">
<temporal>TIMESTAMP</temporal>
</basic>
<basic name="colorId">
<enumerated>ORDINAL</enumerated>
</basic>
<basic name="colorName">
<enumerated>STRING</enumerated>
</basic>
</attributes>
</entity>
Figure 47.12. Temporal/Enum Example Test
@Test
public void testValues() {
log.info("testValues");
ejava.examples.orm.core.annotated.Vase vase = new Vase(1);
Date date = new Date();
vase.setADate(date);
vase.setATime(date);
vase.setATimestamp(date);
vase.setColorId(ColorType.RED);
vase.setColorName(ColorType.RED);
//insert a row in the database
em.persist(vase);
log.info("created case:" + vase);
//find the inserted object
em.flush();
em.clear();
Vase vase2 = em.find(Vase.class, 1L);
log.info("found vase:" + vase2);
-created case:ejava.examples.orm.core.annotated.Vase@7f68f33c, id=1, aDate=Mon Sep 23 00:11:10 EDT 2013, aTime=Mon Sep 23 00:11:10 EDT 2013, aTimestamp=Mon Sep 23 00:11:10 EDT 2013, colorId=RED, colorName=RED -found vase:ejava.examples.orm.core.annotated.Vase@5f0d8b74, id=1, aDate=2013-09-23, aTime=00:11:10, aTimestamp=2013-09-23 00:11:10.296, colorId=RED, colorName=RED
select * from ORMCORE_VASE ID ADATE ATIME ATIMESTAMP COLORID COLORNAME -- ---------- -------- ----------------------------- ------- --------- 1 2006-09-23 14:08:22 2006-09-23 14:08:22.221000000 0 RED
Notice impact of temporal DB-mapping does effect until data saved and retrieved from database