Commit 4b8086f8 authored by liya.cookie's avatar liya.cookie
Browse files

complete broadcast table for raw-jdbc

parent 1b8e8fd7
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ public class DataSourceUtil {
    
    private static final String USER_NAME = "root";
    
    private static final String PASSWORD = "liya76133951";
    private static final String PASSWORD = "";
    
    public static DataSource createDataSource(final String dataSourceName) {
        HikariDataSource result = new HikariDataSource();
+5 −1
Original line number Diff line number Diff line
@@ -17,7 +17,11 @@

package io.shardingsphere.example.common.entity;

public class Country {
import java.io.Serializable;

public class Country implements Serializable {

    private static final long serialVersionUID = 4522167390518926493L;

    private long id;

+38 −0
Original line number Diff line number Diff line
package io.shardingsphere.example.common.jpa.entity;

import io.shardingsphere.example.common.entity.Country;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "t_country")
public class CountryEntity extends Country {

    @Id
    @Column(name = "id")
    @Override
    public long getId() {
        return super.getId();
    }

    @Column(name = "name")
    @Override
    public String getName() {
        return super.getName();
    }

    @Column(name = "code")
    @Override
    public String getCode() {
        return super.getCode();
    }

    @Column(name = "language")
    @Override
    public String getLanguage() {
        return super.getLanguage();
    }
}
+57 −0
Original line number Diff line number Diff line
package io.shardingsphere.example.common.jpa.repository;

import io.shardingsphere.example.common.entity.Country;
import io.shardingsphere.example.common.repository.CountryRepository;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

@Repository
@Transactional
public class CountryRepositoryImpl implements CountryRepository {

    private final static AtomicLong ID_INCREASE = new AtomicLong();

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void createTableIfNotExists() {
        throw new UnsupportedOperationException("createTableIfNotExists for JPA");
    }

    @Override
    public void dropTable() {
        throw new UnsupportedOperationException("truncateTable for JPA");
    }

    @Override
    public void truncateTable() {
        throw new UnsupportedOperationException("dropTable for JPA");
    }

    @Override
    public Long insert(Country country) {
        long id = ID_INCREASE.incrementAndGet();
        country.setId(id);
        entityManager.persist(country);
        return id;
    }

    @Override
    public void delete(Long id) {
        Query query = entityManager.createQuery("DELETE FROM CountryEntity o WHERE o.id = ?1");
        query.setParameter(1, id);
        query.executeUpdate();
    }

    @Override
    public List<Country> selectAll() {
        return (List<Country>) entityManager.createQuery("SELECT o FROM CountryEntity o").getResultList();
    }
}
+67 −5
Original line number Diff line number Diff line
@@ -17,8 +17,11 @@

package io.shardingsphere.example.common.jpa.service;

import io.shardingsphere.example.common.entity.Country;
import io.shardingsphere.example.common.jpa.entity.CountryEntity;
import io.shardingsphere.example.common.jpa.entity.OrderEntity;
import io.shardingsphere.example.common.jpa.entity.OrderItemEntity;
import io.shardingsphere.example.common.repository.CountryRepository;
import io.shardingsphere.example.common.repository.OrderItemRepository;
import io.shardingsphere.example.common.repository.OrderRepository;
import org.springframework.stereotype.Service;
@@ -27,6 +30,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@Service
public class JPACommonServiceImpl implements JPACommonService {
@@ -37,6 +41,9 @@ public class JPACommonServiceImpl implements JPACommonService {
    @Resource
    private OrderItemRepository orderItemRepository;

    @Resource
    private CountryRepository countryRepository;
    
    @Override
    public void initEnvironment() {
    }
@@ -49,9 +56,9 @@ public class JPACommonServiceImpl implements JPACommonService {
    @Transactional
    public void processSuccess() {
        System.out.println("-------------- Process Success Begin ---------------");
        List<Long> orderIds = insertData();
        InsertResult insertResult = insertData();
        printData();
        deleteData(orderIds);
        deleteData(insertResult.getOrderIds(),insertResult.getCountryIds());
        printData();
        System.out.println("-------------- Process Success Finish --------------");
    }
@@ -65,8 +72,14 @@ public class JPACommonServiceImpl implements JPACommonService {
        throw new RuntimeException("Exception occur for transaction test.");
    }

    private List<Long> insertData() {
    private InsertResult insertData(){
        System.out.println("---------------------------- Insert Data ----------------------------");
        List<Long> orderIds = insertOrderData();
        List<Long> countryIds = insertCountryData();
        return new InsertResult(orderIds,countryIds);
    }
    
    private List<Long> insertOrderData() {
        List<Long> result = new ArrayList<>(10);
        for (int i = 1; i <= 10; i++) {
            OrderEntity order = new OrderEntity();
@@ -83,12 +96,37 @@ public class JPACommonServiceImpl implements JPACommonService {
        return result;
    }

    private void deleteData(final List<Long> orderIds) {
    private List<Long> insertCountryData(){
        List<Long> result = new ArrayList<>();
        Locale[] locales = Locale.getAvailableLocales();
        int i = 0;
        for(Locale l:locales){
            final String country = l.getCountry();
            if(country == null || "".equals(country)){
                continue;
            }
            CountryEntity currCountry = new CountryEntity();
            currCountry.setName(l.getDisplayCountry(l));
            currCountry.setLanguage(l.getLanguage());
            currCountry.setCode(l.getCountry());
            countryRepository.insert(currCountry);
            result.add(currCountry.getId());
            if(++i == 10){
                break;
            }
        }
        return result;
    }
    
    private void deleteData(final List<Long> orderIds,final List<Long> countryIds) {
        System.out.println("---------------------------- Delete Data ----------------------------");
        for (Long each : orderIds) {
            orderRepository.delete(each);
            orderItemRepository.delete(each);
        }
        for (Long each: countryIds){
            countryRepository.delete(each);
        }
    }
    
    @Override
@@ -101,5 +139,29 @@ public class JPACommonServiceImpl implements JPACommonService {
        for (Object each : orderItemRepository.selectAll()) {
            System.out.println(each);
        }
        System.out.println("---------------------------- Print Country Data -------------------");
        for (Object each : countryRepository.selectAll()) {
            System.out.println(each);
        }
    }

    private static class InsertResult{

        private List<Long> orderIds;

        private List<Long> countryIds;

        public InsertResult(List<Long> orderIds, List<Long> countryIds) {
            this.orderIds = orderIds;
            this.countryIds = countryIds;
        }

        public List<Long> getOrderIds() {
            return orderIds;
        }

        public List<Long> getCountryIds() {
            return countryIds;
        }
    }
}
Loading