-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[improve] use eclipselink orm replace of hibernate orm (#1801)
Signed-off-by: tomsun28 <tomsun28@outlook.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Logic <zqr10159@dromara.org>
- Loading branch information
1 parent
997bbe3
commit fed2f53
Showing
74 changed files
with
1,786 additions
and
1,335 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,3 +50,4 @@ node_modules | |
# debug env | ||
application-dev.yml | ||
application-mysql.yml | ||
application-pg.yml |
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
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
68 changes: 68 additions & 0 deletions
68
common/src/main/java/org/apache/hertzbeat/common/config/EclipseLinkCustomizer.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,68 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.apache.hertzbeat.common.config; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.eclipse.persistence.config.SessionCustomizer; | ||
import org.eclipse.persistence.descriptors.ClassDescriptor; | ||
import org.eclipse.persistence.mappings.DatabaseMapping; | ||
import org.eclipse.persistence.sessions.Session; | ||
|
||
/** | ||
* change column name that from upper-case to lower-case-with-underscore | ||
*/ | ||
@Slf4j | ||
public class EclipseLinkCustomizer implements SessionCustomizer { | ||
|
||
@Override | ||
public void customize(Session session) throws Exception { | ||
for (ClassDescriptor descriptor : session.getDescriptors().values()) { | ||
for (DatabaseMapping mapping : descriptor.getMappings()) { | ||
if (mapping.isDirectToFieldMapping()) { | ||
// update the column name to lower case with underscore | ||
if (!mapping.getField().getName().equalsIgnoreCase(mapping.getAttributeName())) { | ||
// already custom define the column name, ignore | ||
continue; | ||
} | ||
// todo here i try to change column name that from upper-case to lower-case-with-underscore | ||
// but failed, when update name, something relate exception happen more | ||
mapping.getField().setName(convertCamelCase(mapping.getAttributeName())); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
private String convertCamelCase(String camelCase) { | ||
StringBuilder result = new StringBuilder(); | ||
if (camelCase != null && !camelCase.isEmpty()) { | ||
result.append(Character.toLowerCase(camelCase.charAt(0))); | ||
for (int i = 1; i < camelCase.length(); i++) { | ||
char currentChar = camelCase.charAt(i); | ||
if (Character.isUpperCase(currentChar)) { | ||
result.append('_').append(Character.toLowerCase(currentChar)); | ||
} else { | ||
result.append(currentChar); | ||
} | ||
} | ||
} | ||
return result.toString(); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
common/src/main/java/org/apache/hertzbeat/common/config/EclipseLinkJpaConfiguration.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,61 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.apache.hertzbeat.common.config; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.sql.DataSource; | ||
import org.eclipse.persistence.config.PersistenceUnitProperties; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration; | ||
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter; | ||
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter; | ||
import org.springframework.transaction.jta.JtaTransactionManager; | ||
|
||
/** | ||
* jpa eclipselink impl config | ||
*/ | ||
@Configuration | ||
@EnableAutoConfiguration | ||
public class EclipseLinkJpaConfiguration extends JpaBaseConfiguration { | ||
|
||
protected EclipseLinkJpaConfiguration(DataSource dataSource, JpaProperties properties, | ||
ObjectProvider<JtaTransactionManager> jtaTransactionManager) { | ||
super(dataSource, properties, jtaTransactionManager); | ||
} | ||
|
||
@Override | ||
protected AbstractJpaVendorAdapter createJpaVendorAdapter() { | ||
return new EclipseLinkJpaVendorAdapter(); | ||
} | ||
|
||
@Override | ||
protected Map<String, Object> getVendorProperties() { | ||
HashMap<String, Object> map = new HashMap<>(8); | ||
map.put(PersistenceUnitProperties.DDL_GENERATION, "create-or-extend-tables"); | ||
map.put(PersistenceUnitProperties.SESSION_CUSTOMIZER, "org.apache.hertzbeat.common.config.EclipseLinkCustomizer"); | ||
map.put(PersistenceUnitProperties.ALLOW_NATIVE_SQL_QUERIES, "true"); | ||
map.put(PersistenceUnitProperties.WEAVING, "true"); | ||
return map; | ||
} | ||
} |
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
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
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
Oops, something went wrong.