Search in sources :

Example 11 with Country

use of com.liferay.blade.samples.jdbcservicebuilder.model.Country in project liferay-blade-samples by liferay.

the class CountryPersistenceImpl method create.

/**
 * Creates a new country with the primary key. Does not add the country to the database.
 *
 * @param countryId the primary key for the new country
 * @return the new country
 */
@Override
public Country create(long countryId) {
    Country country = new CountryImpl();
    country.setNew(true);
    country.setPrimaryKey(countryId);
    return country;
}
Also used : CountryImpl(com.liferay.blade.samples.jdbcservicebuilder.model.impl.CountryImpl) Country(com.liferay.blade.samples.jdbcservicebuilder.model.Country)

Example 12 with Country

use of com.liferay.blade.samples.jdbcservicebuilder.model.Country in project liferay-blade-samples by liferay.

the class CountryPersistenceImpl method fetchByPrimaryKeys.

@Override
public Map<Serializable, Country> fetchByPrimaryKeys(Set<Serializable> primaryKeys) {
    if (primaryKeys.isEmpty()) {
        return Collections.emptyMap();
    }
    Map<Serializable, Country> map = new HashMap<Serializable, Country>();
    if (primaryKeys.size() == 1) {
        Iterator<Serializable> iterator = primaryKeys.iterator();
        Serializable primaryKey = iterator.next();
        Country country = fetchByPrimaryKey(primaryKey);
        if (country != null) {
            map.put(primaryKey, country);
        }
        return map;
    }
    Set<Serializable> uncachedPrimaryKeys = null;
    for (Serializable primaryKey : primaryKeys) {
        Serializable serializable = entityCache.getResult(CountryModelImpl.ENTITY_CACHE_ENABLED, CountryImpl.class, primaryKey);
        if (serializable != nullModel) {
            if (serializable == null) {
                if (uncachedPrimaryKeys == null) {
                    uncachedPrimaryKeys = new HashSet<Serializable>();
                }
                uncachedPrimaryKeys.add(primaryKey);
            } else {
                map.put(primaryKey, (Country) serializable);
            }
        }
    }
    if (uncachedPrimaryKeys == null) {
        return map;
    }
    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) + 1);
    query.append(_SQL_SELECT_COUNTRY_WHERE_PKS_IN);
    for (Serializable primaryKey : uncachedPrimaryKeys) {
        query.append((long) primaryKey);
        query.append(StringPool.COMMA);
    }
    query.setIndex(query.index() - 1);
    query.append(StringPool.CLOSE_PARENTHESIS);
    String sql = query.toString();
    Session session = null;
    try {
        session = openSession();
        Query q = session.createQuery(sql);
        for (Country country : (List<Country>) q.list()) {
            map.put(country.getPrimaryKeyObj(), country);
            cacheResult(country);
            uncachedPrimaryKeys.remove(country.getPrimaryKeyObj());
        }
        for (Serializable primaryKey : uncachedPrimaryKeys) {
            entityCache.putResult(CountryModelImpl.ENTITY_CACHE_ENABLED, CountryImpl.class, primaryKey, nullModel);
        }
    } catch (Exception e) {
        throw processException(e);
    } finally {
        closeSession(session);
    }
    return map;
}
Also used : Serializable(java.io.Serializable) Query(com.liferay.portal.kernel.dao.orm.Query) HashMap(java.util.HashMap) StringBundler(com.liferay.portal.kernel.util.StringBundler) NoSuchCountryException(com.liferay.blade.samples.jdbcservicebuilder.exception.NoSuchCountryException) Country(com.liferay.blade.samples.jdbcservicebuilder.model.Country) List(java.util.List) Session(com.liferay.portal.kernel.dao.orm.Session)

Example 13 with Country

use of com.liferay.blade.samples.jdbcservicebuilder.model.Country in project liferay-blade-samples by liferay.

the class CountryPersistenceTest method testDynamicQueryByPrimaryKeyExisting.

@Test
public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
    Country newCountry = addCountry();
    DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Country.class, _dynamicQueryClassLoader);
    dynamicQuery.add(RestrictionsFactoryUtil.eq("countryId", newCountry.getCountryId()));
    List<Country> result = _persistence.findWithDynamicQuery(dynamicQuery);
    Assert.assertEquals(1, result.size());
    Country existingCountry = result.get(0);
    Assert.assertEquals(existingCountry, newCountry);
}
Also used : DynamicQuery(com.liferay.portal.kernel.dao.orm.DynamicQuery) ActionableDynamicQuery(com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery) Country(com.liferay.blade.samples.jdbcservicebuilder.model.Country) Test(org.junit.Test)

Example 14 with Country

use of com.liferay.blade.samples.jdbcservicebuilder.model.Country in project liferay-blade-samples by liferay.

the class CountryPersistenceTest method testCreate.

@Test
public void testCreate() throws Exception {
    long pk = RandomTestUtil.nextLong();
    Country country = _persistence.create(pk);
    Assert.assertNotNull(country);
    Assert.assertEquals(country.getPrimaryKey(), pk);
}
Also used : Country(com.liferay.blade.samples.jdbcservicebuilder.model.Country) Test(org.junit.Test)

Example 15 with Country

use of com.liferay.blade.samples.jdbcservicebuilder.model.Country in project liferay-blade-samples by liferay.

the class CountryPersistenceTest method testFetchByPrimaryKeyMissing.

@Test
public void testFetchByPrimaryKeyMissing() throws Exception {
    long pk = RandomTestUtil.nextLong();
    Country missingCountry = _persistence.fetchByPrimaryKey(pk);
    Assert.assertNull(missingCountry);
}
Also used : Country(com.liferay.blade.samples.jdbcservicebuilder.model.Country) Test(org.junit.Test)

Aggregations

Country (com.liferay.blade.samples.jdbcservicebuilder.model.Country)23 Test (org.junit.Test)13 Serializable (java.io.Serializable)5 NoSuchCountryException (com.liferay.blade.samples.jdbcservicebuilder.exception.NoSuchCountryException)4 ActionableDynamicQuery (com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery)4 Session (com.liferay.portal.kernel.dao.orm.Session)4 CountryImpl (com.liferay.blade.samples.jdbcservicebuilder.model.impl.CountryImpl)3 StringBundler (com.liferay.petra.string.StringBundler)3 DynamicQuery (com.liferay.portal.kernel.dao.orm.DynamicQuery)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Query (com.liferay.portal.kernel.dao.orm.Query)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Function (java.util.function.Function)2 FinderPath (com.liferay.portal.kernel.dao.orm.FinderPath)1 IntegerWrapper (com.liferay.portal.kernel.util.IntegerWrapper)1 StringBundler (com.liferay.portal.kernel.util.StringBundler)1 List (java.util.List)1