Creating Entities

In this tutorial, we will create an persisted entity and its test class.

Setting up environment

You must set up you work environment as described on Configurando seu ambiente. Create the following files at core module:

core/src/main/java/tutorial/StockController.java
core/src/main/java/tutorial/dao/StockDAO.java
core/src/main/java/tutorial/domain/Stock.java
core/src/test/java/tutorial/StockTest.java

Stock.java

public class Stock extends AuthPersistableBusinessObject {

    private Long id;
    private String name;
    private BigDecimal value;
    private BigDecimal unitPrice;
    private String mnemonic;
    private BigDecimal quantity;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_STOCK")
    @Documentation("PRIMARY KEY")
    @Column(name = "PK_STOCK")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Documentation("STOCK NAME")
    @Column(name = "STOCK_NAME")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Documentation("INVESTED STOCK")
    @Column(name = "INVESTED_STOCK", precision = 7, scale = 2)
    public BigDecimal getValue() {
        return value;
    }

    public void setValue(BigDecimal value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return this.name;
    }

    @Documentation("MNEMONIC")
    @Column(name = "MNE")
    public String getMnemonic() {
        return mnemonic;
    }

    public void setMnemonic(String mnemonic) {
        this.mnemonic = mnemonic;
    }

    @Documentation("UNIT_PRICE")
    @Column(name = "UNIT_PRICE", precision = 7, scale = 2)
    public BigDecimal getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(BigDecimal unitPrice) {
        this.unitPrice = unitPrice;
    }

    @Documentation("QUANTITY")
    @Column(name = "QUANTITY", precision = 7)
    public BigDecimal getQuantity() {
        return quantity;
    }

    public void setQuantity(BigDecimal quantity) {
        this.quantity = quantity;
    }
}

StockController.java

public class StockController extends AbstractPersistenceEnabledController {

}

StockDAO.java

public class StockDAO extends BaseDAO<Stock> {

    /**
     * Search stock entities by their names
     *
     * @param name
     * @return a list of stocks
     */
    public List<Stock> findByName(String name) {
        Criteria c = this.createCriteria();
        c.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
        return this.executeQuery(c);
    }

    /**
     * Search stock entities by their mnemonics
     *
     * @param mnemonic
     * @return a list of stocks
     */
    public List<Stock> findByMnemonic(String mnemonic) {
        Criteria c = this.createCriteria();
        c.add(Restrictions.ilike("mnemonic", mnemonic, MatchMode.ANYWHERE));
        return this.executeQuery(c);
    }
}

StockTest.java

public class StockTest extends AuthBOBaseTestCase<Stock> {

    private static final String[] NAMES = {"nome1", "nome2", "nome3"};
    private static final BigDecimal[] VALUES = {
            new BigDecimal(1), new BigDecimal(2), new BigDecimal(3)};

    public static TestSuite suite() {
        return jmine.tec.persist.test.testcase.AuthBOBaseTestCase.getDefaultSuite(StockTest.class);
    }

    @Override
    protected int getTestDataSize() {
        return 3;
    }

    @Override
    protected void fillData(int idx, Stock bo) {
        bo.setName(NAMES[idx]);
        bo.setValue(VALUES[idx]);
    }

    @Override
    protected void compareData(int idx, Stock bo) throws BeanNotFoundException {
        assertEquals(NAMES[idx], bo.getName());
        assertEquals(VALUES[idx], bo.getValue());
    }

    @Override
    protected String getSpringMainXMLFilename() {
        return "core-test-beans.xml";
    }

    @Override
    protected String getControllerSpringId() {
        return "stockController";
    }

    @Override
    protected String[] getResourcesBaseReferencia() {
        return new String[]{};
    }
}

Other files

Some xml files also need to me modified:

  • core/src/main/resources/core-mapping.xml
    • Add a new line on mappingResources list:
<value type="java.lang.Class">tutorial.domain.Stock</value>
  • core/src/main/resources/core-beans.xml
    • We need to set a StockController bean. Add the following line:
<bean id="stockController" class="tutorial.StockController" parent="abstractController"> </bean>

At this point, run StockTest. It should run without errors.