Spring Data JPA Bug Notes

  1. org.hibernate.LazyInitializationException:

Jpa default loading mode for one-to-many entities is lazy loading, i.e. @onetomany see the code

@OneToMany(mappedBy = "pointsAccount",cascade = CascadeType.ALL,fetch = FetchType.LAZY)

Copy the code

Solution: @onetomany (mappedBy = “carFirm”,cascade = CascadeType.ALL,fetch = FetchType.EAGER) private Set brandTypeSet; @manyToOne (cascade = CascadeType.ALL,optional = false,fetch = FetchType.LAZY) @JoinColumn(name=”brandid”,referencedColumnName = “carfirm_id”) private CarFirm carFirm; // A one-to-many relationship between car manufacturer and car brand 3: Spring YML file configuration

The way to attach the original connection www.cnblogs.com/muhy/p/1180… (Thanks to the author but that doesn’t completely solve my problem hh)


jpa:
    show-sql: true
    properties:
      hibernate:
        enable_lazy_load_no_trans: true
        
        format_sql: true
Copy the code

Change the loading mode of one to many parties to load immediately, and one to many parties to load lazily.

  1. Unable to find column with logical name: POINTS_ACCOUNT_ID in org.hibernate.mapping.Table(points_account)

The reason is that WHEN I set the pointsAccount and depositPoints tables, I set the foreign key and needed to use @column to indicate the field name of the member variable in the database. For example, the @column field was missing before modification and JPA could not match the corresponding foreign key relationship

        @Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
//	@Column(name = "POINTS_ACCOUNT_ID")
	private Integer pointsAccountId;
Copy the code

The modified

        @Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "POINTS_ACCOUNT_ID")
	private Integer pointsAccountId;

Copy the code

3.Jpa JQL specification When you need to perform update operations, Jpa provides the save method and JQL specification

Save (S entity) is passed directly into the modified object. The rule is that when the ID of the new object does not exist, modify the remaining fields

The JQL specification syntax is similar to SQL, and is used with the annotations @modifying and @query. The following is an example

  • Table names are poJOs that we define that are associated with tables in the DB
  • Parameter is passed in the format of? Index, index is the parameter position

example

@Query("update PointsAccount p set p.pointsTotal = ? 1 where p.pointsAccountId = ? 2") void updatePointsTotal(Integer pointsTotal,Integer pointsAccountId);Copy the code

!!!!!!!!!! PointsAccount = PointsAccount = PointsAccount = PointsAccount = PointsAccount = PointsAccount = PointsAccount = PointsAccount It’s not the name of the database field but if you put the table name in the database field then you get an exception for example

Example error (if the database table name is points_account) @modifying @query ("update points_account p set P. pointstotal =? 1 where p.pointsAccountId = ? 2") void updatePointsTotal(Integer pointsTotal,Integer pointsAccountId);Copy the code

QuerySyntaxException: Points_account is not mapped into the corresponding SQL statement

Ps: write very mixed wrong place please put forward.