小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Spring Boot Data JPA – 不將實體保存到數(shù)據(jù)庫

 印度阿三17 2019-08-24

我有一個使用JPA的Spring Boot應(yīng)用程序,它有2個數(shù)據(jù)源,1個用于DB2,1個用于SQL Server.

當我嘗試將實體保存到SQL Server時,不會拋出任何錯誤,但實體不會持久保存到數(shù)據(jù)庫.我沒有在日志中看到生成插入內(nèi)容.

提前致謝

這是我為嘗試保存實體而執(zhí)行的代碼.
@零件

public class TestSave {

    @Autowired
    private BeercupMessageLogRepository repository;


    @Scheduled(fixedRate = 500000)
    public void reportCurrentTime() {
        System.out.println("Testing Save ... ");

        // Save the message in the transaction log - TypeId = 1 for Quote
        BeercupMessageLog beercupMessage = new BeercupMessageLog(1,"THIS IS A TEST ...", false);
        beercupMessage = repository.save(beercupMessage);
        System.out.println("Testing save complete ....");

    }
}

這是sql Server配置.

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages="com.xxx.beverage.repository.sqlserver",entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager")
public class sqlserverConfiguration {

    @Bean(name="datasource")
    @Primary
    @ConfigurationProperties(prefix = "sqlserver.datasource")
    public DataSource sqlserverDataSource() {
        return DataSourceBuilder.create().build();
    }

    @PersistenceContext(unitName="primary")
    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean sqlserverEntityManagerFactory(EntityManagerFactoryBuilder builder) {
         return builder.dataSource(sqlserverDataSource()).persistenceUnit("sqlServer").properties(jpaProperties())
                   .packages("com.boelter.beverage.model.sqlserver").build();     }

    private Map<String, Object> jpaProperties() {
         Map<String, Object> props = new HashMap<>();
         props.put("spring.jpa.hibernate.naming-strategy","org.hibernate.cfg.DefaultNamingStrategy");
         props.put("hibernate.default_schema","dbo");
         return props;
     }
}

這是SQL Server存儲庫

公共接口BeercupMessageLogRepository擴展

CrudRepository<BeercupMessageLog, Long> {

    BeercupMessageLog findOne(Long id);
    List<BeercupMessageLog> findByMessageTypeId(Long messageTypeId);
    List<BeercupMessageLog> findAll();

這是DB2配置.

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages="com.boelter.beverage.repository.db2",entityManagerFactoryRef = "entityManagerFactory
2", transactionManagerRef = "transactionManager2")
public class db2Configuration {

    @Bean(name="db2DataSource")
    @ConfigurationProperties(prefix = "db2.datasource")
    public DataSource db2DataSource() {
        return DataSourceBuilder.create().build();
    }

    @PersistenceContext(unitName="secondary")
    @Bean(name = "entityManagerFactory2")
    public LocalContainerEntityManagerFactoryBean db2EntityManagerFactory(EntityManagerFactoryBuilder builder) {
         return builder.dataSource(db2DataSource()).persistenceUnit("db2").properties(jpaProperties())
                   .packages("com.boelter.beverage.model.db2").build();     }

    private Map<String, Object> jpaProperties() {
         Map<String, Object> props = new HashMap<>();
         props.put("spring.jpa.hibernate.naming-strategy","org.hibernate.cfg.DefaultNamingStrategy");
         //props.put("spring.jpa.hibernate.naming-strategy","org.hibernate.cfg.ImprovedNamingStrategy");
         //props.put("spring.jpa.properties.hibernate.default_schema","R3QASDATA");
         //props.put("spring.jpa.show-sql","true");
         return props;
     }
}

這是實體.

package com.boelter.beverage.model.sqlserver;

import java.util.Date;

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

import org.springframework.transaction.annotation.Transactional;
@Table (name="[BeercupMessageLog]")
@Entity
@Transactional
public class BeercupMessageLog {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long    messageId;

    @Column(name="MessageTypeId", nullable = false)
    private long    messageTypeId;

    @Column(name="Processed", nullable = false)
    private boolean processed;

    // Set updatable and insertable to false so JPA does not try to pass a value.  The DB has a default
    // of the current date if no value is passed
    @Column(name="MessageDate", updatable=false, insertable=false)
    private Date    messageDate;

    @Column(name="MessageText", nullable = false)
    private String  messageText;

    protected BeercupMessageLog() {}

    public BeercupMessageLog(long messageTypeId, String messageText, boolean processed) {
        this.messageTypeId = messageTypeId;
        this.messageText = messageText;
        this.processed = processed;
    }

    /**
     * @return the messageId
     */
    public long getMessageId() {
        return messageId;
    }

    /**
     * @param messageId the messageId to set
     */
    public void setMessageId(long messageId) {
        this.messageId = messageId;
    }

    /**
     * @return the messageTypeId
     */
    public long getMessageTypeId() {
        return messageTypeId;
    }

    /**
     * @param messageTypeId the messageTypeId to set
     */
    public void setMessageTypeId(long messageTypeId) {
        this.messageTypeId = messageTypeId;
    }

    /**
     * @return the messageDate
     */
    public Date getMessageDate() {
        return messageDate;
    }

    /**
     * @param messageDate the messageDate to set
     */
    public void setMessageDate(Date messageDate) {
        this.messageDate = messageDate;
    }

    /**
     * @return the processed
     */
    public boolean isProcessed() {
        return processed;
    }

    /**
     * @param processed the processed to set
     */
    public void setProcessed(boolean processed) {
        this.processed = processed;
    }

    /**
     * @return the messageText
     */
    public String getMessageText() {
        return messageText;
    }

    /**
     * @param messageText the messageText to set
     */
    public void setMessageText(String messageText) {
        this.messageText = messageText;
    }

    @Override
    public String toString() {
        return String.format(
                "BeercupMessage[id=%d, typeId=%d, message='%s']",
                messageId, messageTypeId, messageText);
    }

}

解決方法:

我有同樣的問題,對我來說是Spring Batch:

公共類BatchConfiguration擴展DefaultBatchConfigurer {

我擴展DefaultBatchConfigurer的事實是創(chuàng)建了第二個EntityManager,因此我的數(shù)據(jù)源沒有將數(shù)據(jù)持久存儲到數(shù)據(jù)庫中.

來源:https://www./content-2-404651.html

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多