Skip to content
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

[improve] use eclipselink orm replace of hibernate orm #1801

Merged
merged 20 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions alerter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,6 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- data jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>provided</scope>
</dependency>
<!-- validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<scope>provided</scope>
</dependency>
<!-- kafka -->
<dependency>
<groupId>org.apache.kafka</groupId>
Expand Down
5 changes: 0 additions & 5 deletions collector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@
<groupId>org.apache.hertzbeat</groupId>
<artifactId>hertzbeat-remoting</artifactId>
</dependency>
<!-- validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- run sql script-->
<dependency>
<groupId>org.springframework</groupId>
Expand Down
13 changes: 11 additions & 2 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,25 @@
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- jpa -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<scope>provided</scope>
</dependency>
<!-- jackson -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.apache.hertzbeat.common.config;
tomsun28 marked this conversation as resolved.
Show resolved Hide resolved

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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.apache.hertzbeat.common.config;
tomsun28 marked this conversation as resolved.
Show resolved Hide resolved

import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.springframework.beans.factory.ObjectProvider;
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;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
* jpa eclipselink impl config
*/
@Configuration
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.LOGGING_LEVEL,"WARNING");
map.put(PersistenceUnitProperties.ALLOW_NATIVE_SQL_QUERIES, "true");
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@

package org.apache.hertzbeat.common.entity.alerter;

import jakarta.validation.constraints.Size;
import org.apache.hertzbeat.common.util.JsonUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.Length;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
Expand Down Expand Up @@ -55,9 +54,7 @@
public class Alert {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "snow-flake-id")
@GenericGenerator(name = "snow-flake-id",
strategy = "org.apache.hertzbeat.common.util.SnowFlakeIdGenerator")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Schema(title = "Alarm record entity primary key index ID",
description = "Alarm record entity primary key index ID",
example = "87584674384", accessMode = READ_ONLY)
Expand All @@ -66,7 +63,7 @@ public class Alert {
@Schema(title = "Alert target object: monitor availability-available metrics-app.metrics.field",
description = "Alert target object: monitor availability-available metrics-app.metrics.field",
example = "1", accessMode = READ_WRITE)
@Length(max = 255)
@Size(max = 255)
private String target;

@Schema(title = "Alarm definition ID associated with the alarm",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
package org.apache.hertzbeat.common.entity.alerter;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hertzbeat.common.entity.manager.JsonByteListAttributeConverter;
import org.apache.hertzbeat.common.entity.manager.JsonTagListAttributeConverter;
import org.apache.hertzbeat.common.entity.manager.TagItem;
import org.hibernate.validator.constraints.Length;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
Expand Down Expand Up @@ -63,7 +63,7 @@ public class AlertConverge {

@Schema(title = "Policy name", description = "Policy name",
example = "converge-1", accessMode = READ_WRITE)
@Length(max = 100)
@Size(max = 100)
@NotNull
private String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

import com.google.common.base.Objects;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hertzbeat.common.entity.manager.JsonTagListAttributeConverter;
import org.apache.hertzbeat.common.entity.manager.TagItem;
import org.hibernate.validator.constraints.Length;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
Expand Down Expand Up @@ -63,24 +63,24 @@ public class AlertDefine {
private Long id;

@Schema(title = "Monitoring Type", example = "linux", accessMode = READ_WRITE)
@Length(max = 100)
@Size(max = 100)
@NotNull
private String app;

@Schema(title = "Monitoring Metrics", example = "cpu", accessMode = READ_WRITE)
@Length(max = 100)
@Size(max = 100)
@NotNull
private String metric;

@Schema(title = "Monitoring Metrics Field", example = "usage", accessMode = READ_WRITE)
@Length(max = 100)
@Size(max = 100)
private String field;

@Schema(title = "Is Apply All Default", example = "false", accessMode = READ_WRITE)
private boolean preset;

@Schema(title = "Alarm Threshold Expr", example = "usage>90", accessMode = READ_WRITE)
@Length(max = 2048)
@Size(max = 2048)
@Column(length = 2048)
private String expr;

Expand Down Expand Up @@ -111,7 +111,7 @@ public class AlertDefine {

@Schema(title = "Alarm Template", example = "linux {monitor_name}: {monitor_id} cpu usage high",
accessMode = READ_WRITE)
@Length(max = 2048)
@Size(max = 2048)
@Column(length = 2048)
private String template;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
Expand Down Expand Up @@ -75,6 +73,7 @@ public class AlertDefineMonitorBind {
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "monitor_id", referencedColumnName = "id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT),
insertable = false, updatable = false)
@NotFound(action = NotFoundAction.IGNORE)
// todo instead of @NotFound
// @NotFound(action = NotFoundAction.IGNORE)
private Monitor monitor;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
package org.apache.hertzbeat.common.entity.alerter;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hertzbeat.common.entity.manager.JsonByteListAttributeConverter;
import org.apache.hertzbeat.common.entity.manager.JsonTagListAttributeConverter;
import org.apache.hertzbeat.common.entity.manager.TagItem;
import org.hibernate.validator.constraints.Length;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
Expand Down Expand Up @@ -64,7 +64,7 @@ public class AlertSilence {
@Schema(title = "Policy name",
description = "Policy name",
example = "silence-1", accessMode = READ_WRITE)
@Length(max = 100)
@Size(max = 100)
@NotNull
private String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@

package org.apache.hertzbeat.common.entity.alerter;

import jakarta.persistence.Converter;
import org.apache.hertzbeat.common.util.JsonUtil;
import com.fasterxml.jackson.core.type.TypeReference;

import jakarta.persistence.AttributeConverter;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
* json map converter
*/
@Converter
@Component
public class JsonMapAttributeConverter implements AttributeConverter<Map<String, String>, String> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@
package org.apache.hertzbeat.common.entity.manager;

import com.fasterxml.jackson.core.type.TypeReference;
import jakarta.persistence.Converter;
import org.apache.hertzbeat.common.util.JsonUtil;

import jakarta.persistence.AttributeConverter;
import org.springframework.stereotype.Component;

import java.util.List;

/**
* json str to list Byte converter
*/
@Converter
@Component
public class JsonByteListAttributeConverter implements AttributeConverter<List<Byte>, String> {

@Override
Expand Down
Loading
Loading