4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

springで困った時のメモ

Last updated at Posted at 2019-01-08

JPA

saveメソッドを呼び出せば呼び出すほど遅くなる

public void entitySave(List<Entity> eList) {
 for (Entity e : List<Entity> eList) {
  entityRepository.save(e);
 }
}

上記のように大量のデータをinsertする時、処理速度がどんどん遅くなる
image.png

トランザクションを開始していないから遅かった
@Transactionalをつけてあげれば解決

@Transactional
public void entitySave(List<Entity> eList) {
 for (Entity e : List<Entity> eList) {
  entityRepository.save(e);
 }
}

Error creating bean with name 'entityManagerFactory'

ERROR 7184 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: demo.user.User
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1745) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
	

No identifier specified for entity: demo.user.User

entityが識別できないよ!と言われている

エラー対象であるUser.javaは@Entityをつけているにもかかわらず@Idがなかったことが原因

User.java
@Entity
@Data
public class User {
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name = "id")
	private Integer id;
}

@Id をつけてあげると解決

User.java
// org.springframework.data.annotation.Id ← これではない!
import javax.persistence.Id;

@Entity
@Data
public class User {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name = "id")
	private Integer id;
}
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?