반응형
Recent Posts
Recent Comments
관리 메뉴

개발잡부

JPA 본문

JAVA/java

JPA

닉의네임 2020. 4. 23. 19:16
반응형

jpa 

package com.revu.user.config;



import com.revu.user.domain.bmw.CampaignInvitation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
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.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "bmwEntityManager",
        transactionManagerRef = "bmwTransactionManager",
        basePackages = "com.revu.user.domain.bmw"
)
public class BmwDBConfig {

    @Autowired
    private Environment env;

    @Primary
    @Bean(name="bmwDataSource")
    public DataSource bmwDataSource(){

        DriverManagerDataSource bmwDataSource = new DriverManagerDataSource();
        bmwDataSource.setDriverClassName(env.getProperty("spring.kr.datasource.driver-class-name"));
        bmwDataSource.setUrl(env.getProperty("spring.kr.datasource.jdbc-url"));
        bmwDataSource.setUsername(env.getProperty("spring.kr.datasource.username"));
        bmwDataSource.setPassword(env.getProperty("spring.kr.datasource.password"));
        return bmwDataSource;
    }

//    @PersistenceContext(unitName = "CampaignInvitationPU")
    @Bean(name = "bmwEntityManager")
    public LocalContainerEntityManagerFactoryBean bmwEntityManagerFactory(EntityManagerFactoryBuilder builder) {
        return builder
                 .dataSource(bmwDataSource())
                .properties(bmWHibernateProperties())
                .packages(CampaignInvitation.class)
                .persistenceUnit("bmwPU")
                .build();
    }

    @Primary
    @Bean(name="bmwTransactionManager")
    public PlatformTransactionManager bmwTransactionManager(@Qualifier("bmwEntityManager") EntityManagerFactory bmwEntityManagerFactory ){
        return new JpaTransactionManager(bmwEntityManagerFactory);
    }
    private Map bmWHibernateProperties(){
        Resource resource = new ClassPathResource("hibernate.properties");
        try {

            Properties properties = PropertiesLoaderUtils.loadProperties(resource);
            return properties.entrySet().stream().collect(Collectors.toMap(
                    e->e.getKey().toString(),
                    e->e.getValue()
            ));

        } catch (IOException e){
            return new HashMap();
        }
    }
}
package com.revu.user.domain.bmw;

public interface JobMapping {
    Long getId();
    String getValue();
}
package com.revu.user.domain.bmw;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.time.LocalDateTime;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
@Entity
@Table(name = "user_meta_tag")
public class UserMetaTag {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(length = 10, nullable = false)
    private String info;

    @Column(length = 20, nullable = false)
    private String type;

    @Column(length = 20, nullable = false)
    private String value;

    @Column(length = 20, nullable = false)
    private String group;

    @Column(length = 10, nullable = false)
    private Integer seq;

    @Column(length = 1, nullable = false)
    private Integer use;

    @Column(length = 1, nullable = false)
    private Integer input_use;

    @Column(length = 20, nullable = false)
    private LocalDateTime created_at;

    @Column(length = 20, nullable = false)
    private LocalDateTime updated_at;

    @Builder
    public UserMetaTag(String info, String type, String value, String group, Integer seq, Integer use, Integer input_use, LocalDateTime created_at, LocalDateTime updated_at ){
        this.info = info;
        this.type = type;
        this.value = value;
        this.group = group;
        this.seq = seq;
        this.use = use;
        this.input_use = input_use;
        this.created_at = created_at;
        this.updated_at = updated_at;
    }
}
package com.revu.user.domain.bmw;

        import org.springframework.data.domain.Page;
        import org.springframework.data.domain.Pageable;
        import org.springframework.data.jpa.repository.JpaRepository;


public interface UserMetaTagRepository extends JpaRepository<UserMetaTag, Long> {
    Page<JobMapping> findByType(String type, Pageable pageable);
}
package com.revu.user.domain;

import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;


@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseTimeEntity {

    @CreatedDate
    private LocalDateTime created_at;

    @LastModifiedDate
    private LocalDateTime updated_at;
}
package com.revu.user.dto;

import com.revu.user.domain.bmw.CampaignInvitation;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
public class CampaignInvitationDto {

    private Long user_idx;
    private Long campaign_id;

    public CampaignInvitation toEntity() {
        return CampaignInvitation.builder()
                .campaign_id(campaign_id)
                .user_idx(user_idx)
                .build();
    }
}
package com.revu.user.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@AllArgsConstructor(staticName = "create")
public class InvitationDto {
    private Long campaign_id;
    private List<Long> user_idxs;
}
반응형
Comments