---
title: "Yichun Zhao (@minimum) | Hashnode"
description: "Developer. Read the latest articles by Yichun Zhao on Hashnode."
url: "https://hashnode.com/@minimum"
---

@minimum

# Yichun Zhao

@minimum · Joined January 2024

Developer

## Comments

[The error you're encountering suggests a problem with session management in the context of using Hibernate Envers for auditing, particularly when the application attempts to perform operations that require access to session data which is not available. Here's a detailed approach to resolve the issue, focusing on understanding and fixing session management within the context of auditing with Hibernate Envers. Understanding the Error The error message indicates that the application failed to access session data or a defaultSessionRetriever during an audit operation. This typically occurs when there's a mismatch between the transaction/session lifecycle and the point at which Hibernate Envers tries to access session data for auditing purposes. Step-by-Step Solution Review Custom Revision Entity or Listener Configuration If you are using a custom revision entity or revision listener (as indicated by AuditTrailRevisionListener in the stack trace), ensure it's correctly implemented. A common issue is accessing session or transaction-bound resources outside the valid scope. Custom Revision Listener Example: If your AuditTrailRevisionListener implements RevisionListener, it should not directly access transaction-bound resources without ensuring they are available. public class AuditTrailRevisionListener implements RevisionListener { @Override public void newRevision(Object revisionEntity) { // Access session-bound resources carefully here } } Ensure Transaction Management The operation that triggers the audit must be within a transaction. Ensure methods that modify audited entities are annotated with @Transactional or programmatically managed transactions are correctly initiated and completed. @Transactional public void updateEntity(MyEntity entity) { repository.save(entity); } Session Management in Custom Services If SessionDataServiceImpl (mentioned in the stack trace) is part of your application and accesses session data, review its implementation. It should respect the transaction and session lifecycle, obtaining session data in a context-aware manner. Context-aware Session Data Access: Instead of directly accessing session data, consider injecting EntityManager and using it to access the current session indirectly, ensuring it aligns with the current transaction. @Autowired private EntityManager entityManager; public Session getCurrentSession() { return entityManager.unwrap(Session.class); } Integrate defaultSessionRetriever If the error suggests adding a defaultSessionRetriever, it implies your application requires a mechanism to fetch or create session data when not present. This might be specific to your application's architecture. Implementing a session retriever or ensuring that session data is always correctly initialized before your auditing logic runs may be necessary. // Pseudocode for a defaultSessionRetriever implementation public class DefaultSessionRetriever { public SessionData getSessionData() { // Logic to retrieve or create session data } } Review and Adjust Hibernate Envers Configuration Ensure your Hibernate Envers configuration aligns with your application's transaction and session management strategy. Check if there are any properties or configurations specific to Envers that might affect session data availability. Debugging and Logging Increase the logging level for both Spring transactions and Hibernate to get more insight into the transaction and session lifecycle. Look for any anomalies or unexpected behaviors around the time the error occurs. # application.properties logging.level.org.hibernate.envers=DEBUG logging.level.org.springframework.transaction=DEBUG Consult Documentation and Community If the issue persists, consult the Hibernate Envers and Spring documentation for any known issues or guidance on integrating Envers with custom session management strategies. Community forums and Stack Overflow can also be valuable resources for troubleshooting specific issues. Final Note Solving this issue requires a careful review of how your application manages transactions and sessions, especially in the context of auditing with Hibernate Envers. Since the exact solution depends on the specifics of your application's architecture and implementation, the steps provided here are intended to guide you through common areas to check and adjust.](https://hashnode.com/posts/springboot-jpa-error--deleted/65d14e3468b04fda77577b69/comment/65d15308186e2e4ec9d03930)

Comment · Article · Feb 18, 2024 · [SpringBoot JPA error](https://hashnode.com/posts/springboot-jpa-error--deleted/65d14e3468b04fda77577b69)

[The error message you're encountering indicates an issue with a database operation in a Spring-based application. Specifically, the error arises during an attempt to insert a record into the REVINFO table within a schema or database named error-service. The root cause of the error is that the SQL Server database does not recognize the error-service.REVINFO object, meaning the table or the schema/database referenced does not exist or is not accessible in the current database context. Here's a breakdown of the key points from the error message and possible solutions: Error Analysis Error Type: org.springframework.dao.InvalidDataAccessResourceUsageException with org.hibernate.exception.SQLGrammarException caused by com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'error-service.REVINFO'. Operation: An attempt to execute an INSERT statement into the REVINFO table. Context: This is happening within a transaction managed by Spring's JpaTransactionManager, and the error is captured during the transaction commit phase. Possible Solutions Verify Schema/Database Name: Ensure that the schema or database named error-service exists in your SQL Server instance. It's possible that there is a typo in the schema or database name, or the application is connected to a different SQL Server instance where this schema/database does not exist. Check Table Existence: Make sure the REVINFO table exists within the error-service schema/database. If the table is missing, you'll need to create it according to the expected schema required by your application. Database User Permissions: Confirm that the database user your application uses to connect to the SQL Server instance has sufficient permissions to access the error-service schema/database and perform insert operations on the REVINFO table. Lack of permissions could also lead to this error. Configuration Review: Review your Spring application's database configuration settings. Ensure that the data source is correctly configured to point to the appropriate SQL Server instance, database, and schema. This includes checking your Hibernate or JPA configuration for schema or table naming strategies that might be affecting the resolved names at runtime. Hibernate Envers Configuration: If you're using Hibernate Envers for auditing, which seems likely given the reference to REVINFO (a common table name used by Envers for revision information), make sure your Envers setup is correctly configured. This includes ensuring that the audit tables are generated in the expected database/schema. Debugging Steps Database Schema Check: Directly query your SQL Server database to confirm the existence of the error-service schema/database and the REVINFO table. Application Logs: Check the application logs for any additional errors or warnings that might provide more context or indicate configuration issues. Configuration Review: Double-check your application's database connection settings, Hibernate/JPA configuration, and any Envers-specific settings. Addressing the issue will likely involve verifying the database and table existence, ensuring correct configuration settings in your Spring application, and possibly adjusting database permissions.](https://hashnode.com/posts/hibernate-aduit-rev-error--deleted/65cf98d292616f73c534f822/comment/65cf9ac989ae288816aab03a)

Comment · Article · Feb 16, 2024 · [Hibernate Aduit Rev Error](https://hashnode.com/posts/hibernate-aduit-rev-error--deleted/65cf98d292616f73c534f822)

[In SQL Server, the ON \[PRIMARY\] clause in a CREATE TABLE statement specifies which filegroup the table should be placed on. Filegroups are named collections of files and are used to organize and possibly distribute data across multiple files and, potentially, across multiple physical disks. Here's what ON \[PRIMARY\] means in more detail: \[PRIMARY\]: This is the default filegroup in a SQL Server database. When a database is created, the PRIMARY filegroup is automatically created, and it contains the primary data file (.mdf) and any secondary files (.ndf) that are not put into other filegroups. Usage: The clause ON \[PRIMARY\] indicates that the table UnclassifiedErrors should be stored in the primary filegroup of the database. If no filegroup is specified, SQL Server uses the default filegroup, which is PRIMARY in most cases. Performance and Administration: Using filegroups can be a strategic part of database design for performance tuning and for ease of database administration. For example, you might place heavily accessed tables on a filegroup that resides on faster storage, or you might separate out large tables to different filegroups for backup and restore efficiency. TEXTIMAGE\_ON \[PRIMARY\]: This clause is specifically for the large object data types (text, ntext, image, and sometimes varchar(max), nvarchar(max), varbinary(max)). It specifies that the data for these columns will be stored in the PRIMARY filegroup. This is only necessary to specify if you are using a separate filegroup for LOB data and you want to override that and place the LOB data in the primary filegroup. In the context of the script you provided, ON \[PRIMARY\] is used twice: once to specify where the table's data should be stored and once in the TEXTIMAGE\_ON \[PRIMARY\] clause to specify where the large value type data should be stored. Both are indicating the primary filegroup, which is the default behavior if these clauses were omitted (assuming that no other filegroup was set as the default for the database).](https://hashnode.com/posts/ddl-explain--deleted/65cb75f44a90ca5707fca25a/comment/65cb8dc2b14936ce43ae91b5)

Comment · Article · Feb 13, 2024 · [DDL Explain](https://hashnode.com/posts/ddl-explain--deleted/65cb75f44a90ca5707fca25a)
