package blah.blah.blah;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name = "MESSAGE")
public class Message implements Serializable {
@Id
// @GeneratedValue(generator = "system-uuid")
// @GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column(name = "MESSAGE_GUID")
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid")
private String messageGuid;
@Column(name = "MESSAGE_TYPE_CD")
private Long messageTypeCd;
@Column(name = "SENDER_GUID")
private String SenderGuid;
@Column(name = "MESSAGE_TITLE")
private String messageTitle;
@Column(name = "MESSAGE_STATUS_CD")
private Long messageStatusCd;
@Column(name = "ACTIVATED_DT")
private Date activatedDt;
@Column(name = "DEACTIVATED_DT")
private Date deactivatedDt;
@Column(name = "CREATE_BY")
private String createBy;
@Column(name = "CREATE_DT")
private Date createDt;
@Column(name = "UPDATE_BY")
private String updateBy;
@Column(name = "UPDATE_DT")
private Date updateDt;
// required by JPA
public Message() {
}
@SuppressWarnings("all")
public Message(
String messageGuid,
Long messageTypeCd,
String SenderGuid,
String messageTitle,
Long messageStatusCd,
Date activatedDt,
Date deactivatedDt,
String createBy,
Date createDt,
String updateBy,
Date updateDt) {
super();
this.messageGuid = messageGuid;
this.messageTypeCd = messageTypeCd;
this.SenderGuid = SenderGuid;
this.messageTitle = messageTitle;
this.messageStatusCd = messageStatusCd;
this.activatedDt = activatedDt;
this.deactivatedDt = deactivatedDt;
this.createBy = createBy;
this.createDt = createDt;
this.updateBy = updateBy;
this.updateDt = updateDt;
}
public String getMessageGuid() {
return messageGuid;
}
public void setMessageGuid(String messageGuid) {
this.messageGuid = messageGuid;
}
public Long getMessageTypeCd() {
return messageTypeCd;
}
public void setMessageTypeCd(Long messageTypeCd) {
this.messageTypeCd = messageTypeCd;
}
public String getSenderGuid() {
return SenderGuid;
}
public void setSenderGuid(String senderGuid) {
SenderGuid = senderGuid;
}
public String getMessageTitle() {
return messageTitle;
}
public void setMessageTitle(String messageTitle) {
this.messageTitle = messageTitle;
}
public Long getMessageStatusCd() {
return messageStatusCd;
}
public void setMessageStatusCd(Long messageStatusCd) {
this.messageStatusCd = messageStatusCd;
}
public Date getActivatedDt() {
return activatedDt;
}
public void setActivatedDt(Date activatedDt) {
this.activatedDt = activatedDt;
}
public Date getDeactivatedDt() {
return deactivatedDt;
}
public void setDeactivatedDt(Date deactivatedDt) {
this.deactivatedDt = deactivatedDt;
}
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy;
}
public Date getCreateDt() {
return createDt;
}
public void setCreateDt(Date createDt) {
this.createDt = createDt;
}
public String getUpdateBy() {
return updateBy;
}
public void setUpdateBy(String updateBy) {
this.updateBy = updateBy;
}
public Date getUpdateDt() {
return updateDt;
}
public void setUpdateDt(Date updateDt) {
this.updateDt = updateDt;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((SenderGuid == null) ? 0 : SenderGuid.hashCode());
result = prime * result + ((activatedDt == null) ? 0 : activatedDt.hashCode());
result = prime * result + ((createBy == null) ? 0 : createBy.hashCode());
result = prime * result + ((createDt == null) ? 0 : createDt.hashCode());
result = prime * result + ((deactivatedDt == null) ? 0 : deactivatedDt.hashCode());
result = prime * result + ((messageGuid == null) ? 0 : messageGuid.hashCode());
result = prime * result + ((messageStatusCd == null) ? 0 : messageStatusCd.hashCode());
result = prime * result + ((messageTitle == null) ? 0 : messageTitle.hashCode());
result = prime * result + ((messageTypeCd == null) ? 0 : messageTypeCd.hashCode());
result = prime * result + ((updateBy == null) ? 0 : updateBy.hashCode());
result = prime * result + ((updateDt == null) ? 0 : updateDt.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Message other = (Message) obj;
if (SenderGuid == null) {
if (other.SenderGuid != null)
return false;
} else if (!SenderGuid.equals(other.SenderGuid))
return false;
if (activatedDt == null) {
if (other.activatedDt != null)
return false;
} else if (!activatedDt.equals(other.activatedDt))
return false;
if (createBy == null) {
if (other.createBy != null)
return false;
} else if (!createBy.equals(other.createBy))
return false;
if (createDt == null) {
if (other.createDt != null)
return false;
} else if (!createDt.equals(other.createDt))
return false;
if (deactivatedDt == null) {
if (other.deactivatedDt != null)
return false;
} else if (!deactivatedDt.equals(other.deactivatedDt))
return false;
if (messageGuid == null) {
if (other.messageGuid != null)
return false;
} else if (!messageGuid.equals(other.messageGuid))
return false;
if (messageStatusCd == null) {
if (other.messageStatusCd != null)
return false;
} else if (!messageStatusCd.equals(other.messageStatusCd))
return false;
if (messageTitle == null) {
if (other.messageTitle != null)
return false;
} else if (!messageTitle.equals(other.messageTitle))
return false;
if (messageTypeCd == null) {
if (other.messageTypeCd != null)
return false;
} else if (!messageTypeCd.equals(other.messageTypeCd))
return false;
if (updateBy == null) {
if (other.updateBy != null)
return false;
} else if (!updateBy.equals(other.updateBy))
return false;
if (updateDt == null) {
if (other.updateDt != null)
return false;
} else if (!updateDt.equals(other.updateDt))
return false;
return true;
}
@Override
public String toString() {
return "Message [messageGuid=" + messageGuid + ", messageTypeCd=" + messageTypeCd + ", SenderGuid="
+ SenderGuid + ", messageTitle=" + messageTitle + ", messageStatusCd=" + messageStatusCd
+ ", activatedDt=" + activatedDt + ", deactivatedDt=" + deactivatedDt + ", createBy=" + createBy
+ ", createDt=" + createDt + ", updateBy=" + updateBy + ", updateDt=" + updateDt + "]";
}
}
存储库类:
package blah.blah.blah;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface MessageRepository extends JpaRepository<Message, String> {
@Query("SELECT "
+ "m "
+ "FROM Message m "
+ "WHERE messageGuid = :messageGuid")
List<Message> findByMessageGuid(@Param("messageGuid") String messageGuid);
// THIS DOES NOT WORK!
// @Query("INSERT "
// + "INTO Message")
// void insertMessage(@Param("myMessage") Message myMessage);
}
服务类别:
package blah.blah.blah;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MessageServiceImpl implements MessageService {
private static final Logger log = LoggerFactory.getLogger(MessageServiceImpl.class);
@Autowired
private MessageRepository messageRepository;
@Override
public void getMessage(String messageGuid) {
List<Message> messageList = messageRepository.findByMessageGuid(messageGuid);
for (Message message : messageList)
log.info("\n\n" + message.toString() + "\n\n");
}
@Component
public class MessageComponentThingy {
@Autowired
MessageService messageService;
public void thisIsALongExample() {
messageService.getMessage("34A02DCF520F0831E053870910ACED7A");
}
}
我没意识到这有多简单。我已经在我的repository类中继承了JPararePository。JpaRepository有一个名为“save”的方法。所以只需调用该方法。现在一切正常....
我在下面向Service类(插入有一个新的put方法)和Component类(您可以看到我调用Service classes的put方法)添加了新代码。希望这对将来的人有帮助。
实体类:
package blah.blah.blah;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name = "MESSAGE")
public class Message implements Serializable {
@Id
// @GeneratedValue(generator = "system-uuid")
// @GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column(name = "MESSAGE_GUID")
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid")
private String messageGuid;
@Column(name = "MESSAGE_TYPE_CD")
private Long messageTypeCd;
@Column(name = "SENDER_GUID")
private String SenderGuid;
@Column(name = "MESSAGE_TITLE")
private String messageTitle;
@Column(name = "MESSAGE_STATUS_CD")
private Long messageStatusCd;
@Column(name = "ACTIVATED_DT")
private Date activatedDt;
@Column(name = "DEACTIVATED_DT")
private Date deactivatedDt;
@Column(name = "CREATE_BY")
private String createBy;
@Column(name = "CREATE_DT")
private Date createDt;
@Column(name = "UPDATE_BY")
private String updateBy;
@Column(name = "UPDATE_DT")
private Date updateDt;
// required by JPA
public Message() {
}
@SuppressWarnings("all")
public Message(
String messageGuid,
Long messageTypeCd,
String SenderGuid,
String messageTitle,
Long messageStatusCd,
Date activatedDt,
Date deactivatedDt,
String createBy,
Date createDt,
String updateBy,
Date updateDt) {
super();
this.messageGuid = messageGuid;
this.messageTypeCd = messageTypeCd;
this.SenderGuid = SenderGuid;
this.messageTitle = messageTitle;
this.messageStatusCd = messageStatusCd;
this.activatedDt = activatedDt;
this.deactivatedDt = deactivatedDt;
this.createBy = createBy;
this.createDt = createDt;
this.updateBy = updateBy;
this.updateDt = updateDt;
}
public String getMessageGuid() {
return messageGuid;
}
public void setMessageGuid(String messageGuid) {
this.messageGuid = messageGuid;
}
public Long getMessageTypeCd() {
return messageTypeCd;
}
public void setMessageTypeCd(Long messageTypeCd) {
this.messageTypeCd = messageTypeCd;
}
public String getSenderGuid() {
return SenderGuid;
}
public void setSenderGuid(String senderGuid) {
SenderGuid = senderGuid;
}
public String getMessageTitle() {
return messageTitle;
}
public void setMessageTitle(String messageTitle) {
this.messageTitle = messageTitle;
}
public Long getMessageStatusCd() {
return messageStatusCd;
}
public void setMessageStatusCd(Long messageStatusCd) {
this.messageStatusCd = messageStatusCd;
}
public Date getActivatedDt() {
return activatedDt;
}
public void setActivatedDt(Date activatedDt) {
this.activatedDt = activatedDt;
}
public Date getDeactivatedDt() {
return deactivatedDt;
}
public void setDeactivatedDt(Date deactivatedDt) {
this.deactivatedDt = deactivatedDt;
}
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy;
}
public Date getCreateDt() {
return createDt;
}
public void setCreateDt(Date createDt) {
this.createDt = createDt;
}
public String getUpdateBy() {
return updateBy;
}
public void setUpdateBy(String updateBy) {
this.updateBy = updateBy;
}
public Date getUpdateDt() {
return updateDt;
}
public void setUpdateDt(Date updateDt) {
this.updateDt = updateDt;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((SenderGuid == null) ? 0 : SenderGuid.hashCode());
result = prime * result + ((activatedDt == null) ? 0 : activatedDt.hashCode());
result = prime * result + ((createBy == null) ? 0 : createBy.hashCode());
result = prime * result + ((createDt == null) ? 0 : createDt.hashCode());
result = prime * result + ((deactivatedDt == null) ? 0 : deactivatedDt.hashCode());
result = prime * result + ((messageGuid == null) ? 0 : messageGuid.hashCode());
result = prime * result + ((messageStatusCd == null) ? 0 : messageStatusCd.hashCode());
result = prime * result + ((messageTitle == null) ? 0 : messageTitle.hashCode());
result = prime * result + ((messageTypeCd == null) ? 0 : messageTypeCd.hashCode());
result = prime * result + ((updateBy == null) ? 0 : updateBy.hashCode());
result = prime * result + ((updateDt == null) ? 0 : updateDt.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Message other = (Message) obj;
if (SenderGuid == null) {
if (other.SenderGuid != null)
return false;
} else if (!SenderGuid.equals(other.SenderGuid))
return false;
if (activatedDt == null) {
if (other.activatedDt != null)
return false;
} else if (!activatedDt.equals(other.activatedDt))
return false;
if (createBy == null) {
if (other.createBy != null)
return false;
} else if (!createBy.equals(other.createBy))
return false;
if (createDt == null) {
if (other.createDt != null)
return false;
} else if (!createDt.equals(other.createDt))
return false;
if (deactivatedDt == null) {
if (other.deactivatedDt != null)
return false;
} else if (!deactivatedDt.equals(other.deactivatedDt))
return false;
if (messageGuid == null) {
if (other.messageGuid != null)
return false;
} else if (!messageGuid.equals(other.messageGuid))
return false;
if (messageStatusCd == null) {
if (other.messageStatusCd != null)
return false;
} else if (!messageStatusCd.equals(other.messageStatusCd))
return false;
if (messageTitle == null) {
if (other.messageTitle != null)
return false;
} else if (!messageTitle.equals(other.messageTitle))
return false;
if (messageTypeCd == null) {
if (other.messageTypeCd != null)
return false;
} else if (!messageTypeCd.equals(other.messageTypeCd))
return false;
if (updateBy == null) {
if (other.updateBy != null)
return false;
} else if (!updateBy.equals(other.updateBy))
return false;
if (updateDt == null) {
if (other.updateDt != null)
return false;
} else if (!updateDt.equals(other.updateDt))
return false;
return true;
}
@Override
public String toString() {
return "Message [messageGuid=" + messageGuid + ", messageTypeCd=" + messageTypeCd + ", SenderGuid="
+ SenderGuid + ", messageTitle=" + messageTitle + ", messageStatusCd=" + messageStatusCd
+ ", activatedDt=" + activatedDt + ", deactivatedDt=" + deactivatedDt + ", createBy=" + createBy
+ ", createDt=" + createDt + ", updateBy=" + updateBy + ", updateDt=" + updateDt + "]";
}
}
存储库类:
package blah.blah.blah;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface MessageRepository extends JpaRepository<Message, String> {
@Query("SELECT "
+ "m "
+ "FROM Message m "
+ "WHERE messageGuid = :messageGuid")
List<Message> findByMessageGuid(@Param("messageGuid") String messageGuid);
}
服务类别:
package blah.blah.blah;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MessageServiceImpl implements MessageService {
private static final Logger log = LoggerFactory.getLogger(MessageServiceImpl.class);
@Autowired
private MessageRepository messageRepository;
@Override
public void getMessage(String messageGuid) {
List<Message> messageList = messageRepository.findByMessageGuid(messageGuid);
for (Message message : messageList)
log.info("\n\n" + message.toString() + "\n\n");
}
public void putMessage(Message message) {
messageRepository.save(message);
System.out.println("\n\nInside putMessage\n\n");
}
}
@Component
public class MessageComponentThingy {
@Autowired
MessageService messageService;
public void thisIsALongExample() {
messageService.getMessage("34A02DCF520F0831E053870910ACED7A");
Message message = new Message();
message.setMessageTypeCd(1L);
message.setSenderGuid("test");
message.setMessageTitle("test title");
message.setMessageStatusCd(1L);
message.setActivatedDt(new Date());
message.setDeactivatedDt(new Date());
message.setCreateBy("me");
message.setCreateDt(new Date());
message.setUpdateBy("me");
message.setUpdateDt(new Date());
messageService.putMessage(message);
}
}
问题内容: 我有一个测试用例,需要将100‘000个实体实例持久存储到数据库中。我当前使用的代码可以做到这一点,但是要花40秒才能将所有数据持久保存在数据库中。从大小约为15 MB的JSON文件中读取数据。 现在,在另一个项目之前,我已经在自定义存储库中实现了批量插入方法。但是,在那种情况下,我要保留许多顶级实体,而只有少数嵌套实体。 在我目前的情况下,我有5个实体,其中包含约30个实体的列表。其
我有一个测试用例,需要将100,000个实体实例持久化到数据库中。我当前使用的代码就是这样做的,但直到将所有数据持久化到数据库中为止,最多需要40秒。数据是从一个大约15 MB大小的JSON文件中读取的。 现在,我已经为另一个项目在自定义存储库中实现了批处理插入方法。但是,在这种情况下,我有很多顶级实体要持久化,只有几个嵌套实体。 在我当前的例子中,我有5个实体,这些实体包含大约30个实体的列表。
我希望强制CrudRepository#save(entity)插入一个新实体,而不是先选择实体,然后在主键已经存在的情况下更新它。
假设我有两个实体: 是否可以这样做: 即:接受一个实体(本例中为< code>Address)作为参数,但在原生查询中引用它的一个属性(本例中为< code>Address.street? 由于各种原因,我不能对这两个实体使用“正常”和JPA查询。
本文向大家介绍Spring Boot整合Spring Data Jpa代码实例,包括了Spring Boot整合Spring Data Jpa代码实例的使用技巧和注意事项,需要的朋友参考一下 一、Spring Data Jpa的简介 spring data:其实就是spring 提供的一个操作数据的框架。而spring data JPA 只是spring data 框架下的一个基于JPA标准操作数
主要内容:JPA实体插入示例在JPA中,我们可以通过实体轻松地将数据插入到数据库中。 提供方法来插入数据记录。 JPA实体插入示例 在这里,我们将演示如何使用实现插入学生的记录。 创建一个JPA项目,其项目目录结构如下所示 - 这个例子包含以下步骤 - 第1步: 在包下创建一个名为StudentEntity.java的实体类,这个类包含以下几个属性:, 和 。 文件:StudentEntity.java 的代码如下 - 第2