春天自动保存

时间:2016-02-17 19:02:32

标签: java spring hibernate spring-data spring-data-jpa

我正在使用带有jpa的spring并且希望我的实体表能够自动保存,但它们不会。

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class DatabaseConfig {

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan(new String[] { "com.project.models" });

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);

        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        em.setJpaProperties(properties);        
        return em;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.h2.jdbcx.JdbcDataSource");
        dataSource.setUrl("jdbc:h2:./store");
        dataSource.setUsername("root");
        return dataSource;
    }
}

当一个新的客户端连接它在这个类中处理但我希望它保存它们而不调用repository.save方法任何帮助?

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import io.horizon.model.ClientModel;
import io.horizon.net.packet.Packet;
import io.horizon.net.packet.PacketHandler;
import io.netty.channel.ChannelHandlerContext;

@Component
public class ClientInfoPacketHandler implements PacketHandler {

    @Resource
    private Map<String, ClientModel> clients;

    @Override
    public void handlePacket(ChannelHandlerContext ctx, Packet packet) {
        ClientModel clientModel = new ClientModel(packet.getString());
        switch(packet.getString()) {
        case "init":
            clientModel.setStatus(1);
            break;
        }

        //This should be saved but it doesn't unless i use the clientRepository to call the save method.
    }

}

ClientModel类:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import lombok.Data;

@Data
@Entity
@Table(name = "clients")
public class ClientModel {

    @GeneratedValue
    private long id;

    private int status;

    @Id
    private String uid;

    public ClientModel() {      
    }

    public ClientModel(String uid) {        
        this.uid = uid;
    }   

}

0 个答案:

没有答案