Archived
0

Hibernate -> EclipseLink

потому что Hibernate как библиотека занимает много места
This commit is contained in:
2019-02-11 11:59:36 +03:00
parent f08190fba7
commit 3ce8aa878a
5 changed files with 45 additions and 17 deletions

View File

@@ -9,9 +9,12 @@ dependencies {
compile_excludeCopy project(':core')
/* Spring */
compile (group: 'org.springframework.data', name: 'spring-data-jpa', version: spring_data_version)
compile (group: 'org.springframework.data', name: 'spring-data-jpa', version: spring_data_version) {
exclude (group: 'org.hibernate', module: 'hibernate-entitymanager')
exclude (group: 'org.hibernate', module: 'hibernate-core')
}
/* Database */
compile (group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.3.6.Final')
compile (group: 'com.h2database', name: 'h2', version: '1.4.197')
compile (group: 'org.eclipse.persistence', name: 'org.eclipse.persistence.jpa', version: '2.7.0')
}

View File

@@ -5,7 +5,6 @@ import lombok.NoArgsConstructor;
import mc.core.EntityLocation;
import mc.core.h2db.H2Player;
import mc.core.world.World;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.context.ApplicationContext;
import javax.persistence.*;
@@ -20,7 +19,6 @@ import java.util.UUID;
public class H2PlayerEntity {
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name= "increment", strategy= "increment")
@Column(nullable = false)
private Long id;

View File

@@ -1,9 +1,11 @@
package mc.core.h2db;
import mc.core.EntityLocation;
import mc.core.h2db.repository.H2PlayerEntityRepository;
import mc.core.h2db.service.H2PlayerService;
import mc.core.player.Player;
import mc.core.world.World;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -19,13 +21,24 @@ import static org.junit.jupiter.api.Assertions.*;
@ContextConfiguration(classes = {TestSpringConfig.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class H2PlayerManagerTest {
@Autowired
private H2PlayerService h2PlayerService;
@Autowired
private H2PlayerEntityRepository h2PlayerEntityRepository;
@Autowired
private World mockWorld;
@Autowired
private H2PlayerManager playerManager;
@BeforeEach
void before() {
h2PlayerEntityRepository.deleteAll();
}
@Test
void createPlayer() {
final String playerName = "NEW_PLAYER";

View File

@@ -2,7 +2,7 @@ package mc.core.h2db;
import mc.core.h2db.service.H2PlayerService;
import mc.core.world.World;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@@ -10,6 +10,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManagerFactory;
@@ -29,21 +30,22 @@ public class TestSpringConfig {
private static final String DATABASE_USERNAME = "sa";
private static final String DATABASE_PASSWORD = "s3cReT";
static {
System.setProperty("org.jboss.logging.provider", "slf4j");
}
private Properties hibernateProp() {
private Properties eclipseLinkProp() {
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.format_sql", "true");
properties.put("hibernate.use_sql_comments", "true");
properties.put("hibernate.hbm2ddl.auto", "create");
properties.put(PersistenceUnitProperties.WEAVING, "static");
return properties;
}
private EclipseLinkJpaVendorAdapter getEclipseLinkJpaVendorAdapter() {
EclipseLinkJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
vendorAdapter.setDatabasePlatform("org.eclipse.persistence.platform.database.H2Platform");
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setShowSql(true);
return vendorAdapter;
}
@Bean("mockWorld")
public World mockWorld() {
World mockWorld = mock(World.class);
@@ -66,9 +68,9 @@ public class TestSpringConfig {
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
entityManagerFactoryBean.setJpaVendorAdapter(getEclipseLinkJpaVendorAdapter());
entityManagerFactoryBean.setPackagesToScan("mc.core.h2db.entity");
entityManagerFactoryBean.setJpaProperties(hibernateProp());
entityManagerFactoryBean.setJpaProperties(eclipseLinkProp());
return entityManagerFactoryBean;
}

View File

@@ -3,7 +3,9 @@ package mc.core.h2db.service;
import mc.core.EntityLocation;
import mc.core.h2db.H2Player;
import mc.core.h2db.TestSpringConfig;
import mc.core.h2db.repository.H2PlayerEntityRepository;
import mc.core.world.World;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -20,8 +22,13 @@ import static org.junit.jupiter.api.Assertions.*;
@ContextConfiguration(classes = {TestSpringConfig.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class H2PlayerServiceTest {
@Autowired
private H2PlayerService h2PlayerService;
@Autowired
private H2PlayerEntityRepository h2PlayerEntityRepository;
@Autowired
private World world;
@@ -54,6 +61,11 @@ class H2PlayerServiceTest {
assertEquals(expected.getWorld(), actual.getWorld());
}
@BeforeEach
void before() {
h2PlayerEntityRepository.deleteAll();
}
@Test
void save() {
H2Player player = buildPlayer();