Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
commit
376206389f
|
@ -6,71 +6,68 @@ import org.apache.commons.lang.ArrayUtils;
|
|||
|
||||
public class ArrayInitializer {
|
||||
|
||||
public static int[] initializeArrayInLoop() {
|
||||
static int[] initializeArrayInLoop() {
|
||||
int array[] = new int[5];
|
||||
for (int i = 0; i < array.length; i++)
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
array[i] = i + 2;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public static int[][] initializeMultiDimensionalArrayInLoop() {
|
||||
static int[][] initializeMultiDimensionalArrayInLoop() {
|
||||
int array[][] = new int[2][5];
|
||||
for (int i = 0; i < 2; i++)
|
||||
for (int j = 0; j < 5; j++)
|
||||
for (int i = 0; i < 2; i++) {
|
||||
for (int j = 0; j < 5; j++) {
|
||||
array[i][j] = j + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
public static String[] initializeArrayAtTimeOfDeclarationMethod1() {
|
||||
String array[] = new String[] { "Toyota", "Mercedes", "BMW", "Volkswagen", "Skoda" };
|
||||
static String[] initializeArrayAtTimeOfDeclarationMethod1() {
|
||||
String array[] = new String[]{"Toyota", "Mercedes", "BMW", "Volkswagen", "Skoda"};
|
||||
return array;
|
||||
}
|
||||
|
||||
public static int[] initializeArrayAtTimeOfDeclarationMethod2() {
|
||||
int[] array = new int[] { 1, 2, 3, 4, 5 };
|
||||
static int[] initializeArrayAtTimeOfDeclarationMethod2() {
|
||||
int[] array = new int[]{1, 2, 3, 4, 5};
|
||||
return array;
|
||||
}
|
||||
|
||||
public static int[] initializeArrayAtTimeOfDeclarationMethod3() {
|
||||
int array[] = { 1, 2, 3, 4, 5 };
|
||||
static int[] initializeArrayAtTimeOfDeclarationMethod3() {
|
||||
int array[] = {1, 2, 3, 4, 5};
|
||||
return array;
|
||||
}
|
||||
|
||||
public static long[] initializeArrayUsingArraysFill() {
|
||||
static long[] initializeArrayUsingArraysFill() {
|
||||
long array[] = new long[5];
|
||||
Arrays.fill(array, 30);
|
||||
return array;
|
||||
}
|
||||
|
||||
public static int[] initializeArrayRangeUsingArraysFill() {
|
||||
static int[] initializeArrayRangeUsingArraysFill() {
|
||||
int array[] = new int[5];
|
||||
Arrays.fill(array, 0, 3, -50);
|
||||
return array;
|
||||
}
|
||||
|
||||
public static int[] initializeArrayUsingArraysCopy() {
|
||||
int array[] = { 1, 2, 3, 4, 5 };
|
||||
static int[] initializeArrayUsingArraysCopy() {
|
||||
int array[] = {1, 2, 3, 4, 5};
|
||||
int[] copy = Arrays.copyOf(array, 5);
|
||||
return copy;
|
||||
}
|
||||
|
||||
public static int[] initializeLargerArrayUsingArraysCopy() {
|
||||
int array[] = { 1, 2, 3, 4, 5 };
|
||||
static int[] initializeLargerArrayUsingArraysCopy() {
|
||||
int array[] = {1, 2, 3, 4, 5};
|
||||
int[] copy = Arrays.copyOf(array, 6);
|
||||
return copy;
|
||||
}
|
||||
|
||||
public static int[] initializeArrayUsingArraysSetAll() {
|
||||
static int[] initializeArrayUsingArraysSetAll() {
|
||||
int[] array = new int[20];
|
||||
|
||||
for (int i = 0; i < 20; i++) {
|
||||
Arrays.setAll(array, p -> {
|
||||
if (p > 9)
|
||||
return 0;
|
||||
else
|
||||
return p;
|
||||
});
|
||||
}
|
||||
Arrays.setAll(array, p -> p > 9 ? 0 : p);
|
||||
return array;
|
||||
}
|
||||
|
||||
|
|
|
@ -141,7 +141,7 @@
|
|||
<!-- persistence -->
|
||||
<hibernate.version>3.6.10.Final</hibernate.version>
|
||||
<mysql-connector-java.version>5.1.40</mysql-connector-java.version>
|
||||
<tomcat-dbcp.version>7.0.73</tomcat-dbcp.version>
|
||||
<tomcat-dbcp.version>8.5.8</tomcat-dbcp.version>
|
||||
<h2.version>1.4.193</h2.version>
|
||||
|
||||
<!-- various -->
|
||||
|
@ -162,4 +162,4 @@
|
|||
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
|
||||
|
||||
import org.baeldung.persistence.model.Event;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public class EventDao extends AbstractHibernateDao<Event> implements IEventDao {
|
||||
|
||||
public EventDao() {
|
||||
super();
|
||||
|
||||
setClazz(Event.class);
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package org.baeldung.persistence.dao;
|
||||
|
||||
import org.baeldung.persistence.model.Event;
|
||||
|
||||
|
||||
|
||||
public interface IEventDao extends IOperations<Event> {
|
||||
//
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package org.baeldung.persistence.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "EVENTS")
|
||||
public class Event implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String description;
|
||||
|
||||
public Event() {
|
||||
}
|
||||
|
||||
|
||||
public Event(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package org.baeldung.persistence.service;
|
||||
|
||||
|
||||
import org.baeldung.persistence.dao.IEventDao;
|
||||
import org.baeldung.persistence.model.Event;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class EventService {
|
||||
|
||||
@Autowired
|
||||
private IEventDao dao;
|
||||
|
||||
public EventService() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public void create(final Event entity) {
|
||||
dao.create(entity);
|
||||
}
|
||||
|
||||
}
|
|
@ -4,7 +4,7 @@ import java.util.Properties;
|
|||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
|
||||
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
@ -75,4 +75,4 @@ public class PersistenceConfig {
|
|||
return hibernateProperties;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE hibernate-configuration PUBLIC
|
||||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
|
||||
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
|
||||
<hibernate-configuration>
|
||||
<session-factory>
|
||||
<mapping class="org.baeldung.persistence.model.Event" />
|
||||
</session-factory>
|
||||
</hibernate-configuration>
|
|
@ -0,0 +1,58 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:jee="http://www.springframework.org/schema/jee"
|
||||
xmlns:lang="http://www.springframework.org/schema/lang"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xmlns:util="http://www.springframework.org/schema/util"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
|
||||
|
||||
<context:annotation-config />
|
||||
<context:component-scan base-package="org.baeldung.persistence" />
|
||||
<context:property-placeholder location="classpath:persistence-mysql.properties"/>
|
||||
|
||||
|
||||
|
||||
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
|
||||
<property name="driverClassName" value="${jdbc.driverClassName}"/>
|
||||
<property name="url" value="${jdbc.url}"/>
|
||||
<property name="username" value="${jdbc.user}"/>
|
||||
<property name="password" value="${jdbc.pass}"/>
|
||||
</bean>
|
||||
|
||||
|
||||
<bean id="sessionFactory"
|
||||
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
|
||||
<property name="dataSource" ref="dataSource" />
|
||||
<property name="configLocation">
|
||||
<value>classpath:exceptionDemo.cfg.xml</value>
|
||||
</property>
|
||||
<property name="hibernateProperties">
|
||||
<props>
|
||||
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
|
||||
<prop key="hibernate.show_sql">true</prop>
|
||||
<prop key="hibernate.hbm2ddl.auto">create</prop>
|
||||
<!-- <prop key="hibernate.current_session_context_class">thread</prop>-->
|
||||
</props>
|
||||
</property>
|
||||
|
||||
</bean>
|
||||
|
||||
<!-- <bean id="employeeDAO" class="com.howtodoinjava.dao.EmployeeDaoImpl"></bean> -->
|
||||
<!-- <bean id="employeeManager" class="com.howtodoinjava.service.EmployeeManagerImpl"></bean> -->
|
||||
|
||||
<tx:annotation-driven transaction-manager="transactionManager"/>
|
||||
<bean id="transactionManager"
|
||||
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
|
||||
<property name="sessionFactory" ref="sessionFactory" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
|
@ -18,7 +18,7 @@
|
|||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
|
||||
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
|
||||
<property name="driverClassName" value="${jdbc.driverClassName}"/>
|
||||
<property name="url" value="${jdbc.url}"/>
|
||||
<property name="username" value="${jdbc.user}"/>
|
||||
|
@ -31,4 +31,4 @@
|
|||
|
||||
<bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
|
||||
|
||||
</beans>
|
||||
</beans>
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
package org.baeldung.persistence.service;
|
||||
|
||||
import org.baeldung.persistence.model.Event;
|
||||
import org.baeldung.spring.PersistenceXmlConfig;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.orm.hibernate3.HibernateSystemException;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceXmlConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class NoHibernateSessBoundUsingAnnoSessionBeanMainIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
EventService service;
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedEx = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public final void whenEntityIsCreated_thenNoExceptions() {
|
||||
service.create(new Event("from Annotation Session Bean Factory"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public final void whenNoTransBoundToSession_thenException() {
|
||||
expectedEx.expect(HibernateSystemException.class);
|
||||
expectedEx.expectMessage("No Hibernate Session bound to thread, "
|
||||
+ "and configuration does not allow creation of "
|
||||
+ "non-transactional one here");
|
||||
service.create(new Event("from Annotation Session Bean Factory"));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package org.baeldung.persistence.service;
|
||||
|
||||
import org.baeldung.persistence.model.Event;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.orm.hibernate3.HibernateSystemException;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "classpath:exceptionDemoPersistenceConfig.xml" })
|
||||
public class NoHibernateSessBoundUsingLocalSessionBeanMainIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
EventService service;
|
||||
|
||||
@Rule
|
||||
public ExpectedException expectedEx = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public final void whenEntityIsCreated_thenNoExceptions() {
|
||||
service.create(new Event("from local session bean factory"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public final void whenNoTransBoundToSession_thenException() {
|
||||
expectedEx.expect(HibernateException.class);
|
||||
expectedEx.expectMessage("No Hibernate Session bound to thread, "
|
||||
+ "and configuration does not allow creation "
|
||||
+ "of non-transactional one here");
|
||||
service.create(new Event("from local session bean factory"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue