-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate UserType
implementations to hibernate 6
#40
Draft
Laurens-W
wants to merge
10
commits into
main
Choose a base branch
from
usertype
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+456
−4
Draft
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
53d9380
WIP
Laurens-W 632d557
WIP
Laurens-W e1c596d
WIP
Laurens-W 49866e4
WIP
Laurens-W f30a147
WIP
Laurens-W 7eaa3c3
WIP
Laurens-W 506b306
Apply suggestions from code review
Laurens-W a6db72b
New line at end of file
Laurens-W 53a0933
imports
Laurens-W 6c868a6
No need to add hibernate-core-5 to src/main
timtebeek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
252 changes: 252 additions & 0 deletions
252
src/main/java/org/openrewrite/hibernate/MigrateUserType.java
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file added
BIN
+7.16 MB
src/main/resources/META-INF/rewrite/classpath/hibernate-core-5.6.15.Final.jar
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
src/test/java/org/openrewrite/hibernate/MigrateUserTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.hibernate; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.InMemoryExecutionContext; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class MigrateUserTypeTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new MigrateUserType()) | ||
.parser(JavaParser.fromJavaVersion() | ||
.classpathFromResources(new InMemoryExecutionContext(), "hibernate-core-5+") | ||
); | ||
} | ||
|
||
@Test | ||
@DocumentExample | ||
void shouldMigrateUserType() { | ||
//language=java | ||
rewriteRun( | ||
spec -> spec.expectedCyclesThatMakeChanges(2), | ||
java( | ||
""" | ||
import org.hibernate.HibernateException; | ||
import org.hibernate.engine.spi.SharedSessionContractImplementor; | ||
import org.hibernate.usertype.UserType; | ||
import java.io.Serializable; | ||
import java.math.BigDecimal; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Types; | ||
import java.util.Objects; | ||
public class BigDecimalAsString implements UserType { | ||
@Override | ||
public int[] sqlTypes() { | ||
return new int[]{Types.VARCHAR}; | ||
} | ||
@Override | ||
public Class returnedClass() { | ||
return BigDecimal.class; | ||
} | ||
@Override | ||
public boolean equals(Object x, Object y) { | ||
return Objects.equals(x, y); | ||
} | ||
@Override | ||
public int hashCode(Object x) { | ||
return Objects.hashCode(x); | ||
} | ||
@Override | ||
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws SQLException { | ||
String string = rs.getString(names[0]); | ||
return string == null || rs.wasNull() ? null : new BigDecimal(string); | ||
} | ||
@Override | ||
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws SQLException { | ||
if (value == null) { | ||
st.setNull(index, Types.VARCHAR); | ||
} else { | ||
st.setString(index, value.toString()); | ||
} | ||
} | ||
@Override | ||
public Object deepCopy(Object value) { | ||
return value; | ||
} | ||
@Override | ||
public boolean isMutable() { | ||
return false; | ||
} | ||
@Override | ||
public Serializable disassemble(Object value) { | ||
return (BigDecimal) value; | ||
} | ||
@Override | ||
public Object assemble(Serializable cached, Object owner) { | ||
return cached; | ||
} | ||
@Override | ||
public Object replace(Object original, Object target, Object owner) { | ||
return original; | ||
} | ||
} | ||
""", | ||
""" | ||
import org.hibernate.HibernateException; | ||
import org.hibernate.engine.spi.SharedSessionContractImplementor; | ||
import org.hibernate.usertype.UserType; | ||
import java.io.Serializable; | ||
import java.math.BigDecimal; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Types; | ||
import java.util.Objects; | ||
public class BigDecimalAsString implements UserType<BigDecimal> { | ||
@Override | ||
public int getSqlType() { | ||
return Types.VARCHAR; | ||
} | ||
@Override | ||
public Class<BigDecimal> returnedClass() { | ||
return BigDecimal.class; | ||
} | ||
@Override | ||
public boolean equals(BigDecimal x, BigDecimal y) { | ||
return Objects.equals(x, y); | ||
} | ||
@Override | ||
public int hashCode(BigDecimal x) { | ||
return Objects.hashCode(x); | ||
} | ||
@Override | ||
public BigDecimal nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException { | ||
String string = rs.getString(position); | ||
return string == null || rs.wasNull() ? null : new BigDecimal(string); | ||
} | ||
@Override | ||
public void nullSafeSet(PreparedStatement st, BigDecimal value, int index, SharedSessionContractImplementor session) throws SQLException { | ||
if (value == null) { | ||
st.setNull(index, Types.VARCHAR); | ||
} else { | ||
st.setString(index, value.toString()); | ||
} | ||
} | ||
@Override | ||
public BigDecimal deepCopy(BigDecimal value) { | ||
return value; | ||
} | ||
@Override | ||
public boolean isMutable() { | ||
return false; | ||
} | ||
@Override | ||
public Serializable disassemble(BigDecimal value) { | ||
return value; | ||
} | ||
@Override | ||
public BigDecimal assemble(Serializable cached, Object owner) { | ||
return (BigDecimal) cached; | ||
} | ||
@Override | ||
public BigDecimal replace(BigDecimal original, BigDecimal target, Object owner) { | ||
return original; | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} | ||
Laurens-W marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally we fix the recipe such that we don't need this escape hatch, as we'd love to drop that in a next major version. Did you explore why this was needed in the first place?