Java 弹簧自动保存
我在jpa中使用spring,希望我的实体表自动保存,但它们没有Java 弹簧自动保存,java,spring,hibernate,spring-data,spring-data-jpa,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.Pe
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
类看起来像什么?对你来说是个坏消息:你必须手动持久化它。添加了ClientModel类。调用save方法是持久化实体的方法。我想知道你们不想使用它的方法…因为我想自动保存,一定有什么方法吗?你们的ClientModel
类看起来像什么?坏消息:你们必须手动保存它。添加了ClientModel类。调用save方法是保存实体的方法。我想知道你们不想用它的方式…因为我想自动保存,一定有办法吗?
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;
}
}