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

refact: unified primary key annotation. #334

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
//
// This source code is licensed under Apache 2.0 License.

import org.nebula.contrib.ngbatis.annotations.base.GraphId;
import javax.persistence.Id;
import org.nebula.contrib.ngbatis.annotations.base.Tag;
import org.nebula.contrib.ngbatis.base.GraphBaseVertex;
import org.nebula.contrib.ngbatis.enums.IdType;


/**
Expand All @@ -19,7 +18,7 @@
@Tag(name = "player")
public class Player extends GraphBaseVertex {

@GraphId(type = IdType.STRING)
@Id
private String id;

private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
//
// This source code is licensed under Apache 2.0 License.

import org.nebula.contrib.ngbatis.annotations.base.GraphId;
import javax.persistence.Id;
import org.nebula.contrib.ngbatis.annotations.base.Tag;
import org.nebula.contrib.ngbatis.base.GraphBaseVertex;
import org.nebula.contrib.ngbatis.enums.IdType;


/**
Expand All @@ -19,7 +18,7 @@
@Tag(name = "team")
public class Team extends GraphBaseVertex {

@GraphId(type = IdType.STRING)
@Id
private String id;

private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ public void dynamicSpaceWithPage() {
void assertSpaceFailed(Exception e) {
e.printStackTrace();
String message = e.getMessage();
Assert.isTrue(e instanceof QueryException &&
(message.contains("SpaceNotFound") || (message.contains("create session failed.")))
Assert.isTrue(e instanceof QueryException
&& (message.contains("SpaceNotFound") || (message.contains("create session failed.")))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author xYLiuuuuuu
* @since 2024/9/8 10:14
*/

@Deprecated
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface GraphId {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.Id;
import org.nebula.contrib.ngbatis.ArgsResolver;
import org.nebula.contrib.ngbatis.Env;
import org.nebula.contrib.ngbatis.ResultResolver;
import org.nebula.contrib.ngbatis.SessionDispatcher;
import org.nebula.contrib.ngbatis.annotations.base.EdgeType;
import org.nebula.contrib.ngbatis.annotations.base.GraphId;
import org.nebula.contrib.ngbatis.annotations.base.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -155,8 +155,8 @@ public static <T extends GraphBaseVertex> Map<String, Object> getV2Property(T v2
continue; // 跳过初始值
}
}
// 处理带有 @GraphId 注解的字段
if (field.isAnnotationPresent(GraphId.class)) {
// 处理带有 @Id 注解的字段
if (field.isAnnotationPresent(Id.class)) {
result.put("id", fieldValue);
} else {
result.put(getNameByColumn(field), fieldValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.nebula.contrib.ngbatis.annotations.base.GraphId;
import javax.persistence.Id;
import org.nebula.contrib.ngbatis.annotations.base.Tag;
import org.nebula.contrib.ngbatis.enums.Direction;
import org.nebula.contrib.ngbatis.enums.IdType;
Expand Down Expand Up @@ -355,8 +355,8 @@ protected Map<String, Object> getEntityProperties() {
continue; // 跳过初始值
}
}
// 处理带有 @GraphId 注解的字段
if (field.isAnnotationPresent(GraphId.class)) {
// 处理带有 @Id 注解的字段
if (field.isAnnotationPresent(Id.class)) {
properties.put("id", value);
} else {
properties.put(getNameByColumn(field), value);
Expand All @@ -377,14 +377,14 @@ private Object getVertexId() {
for (Field field : declaredFields) {
Annotation[] annotations = field.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof GraphId) {
if (annotation instanceof Id) {
id = getValue(this, field);
specificTypeField = field.getType();
}
}
}
if (id == null || isPrimitiveDefaultValue(specificTypeField,id)) {
throw new RuntimeException(entityClass.getSimpleName() + " does not have @GraphId");
throw new RuntimeException(entityClass.getSimpleName() + " does not have @Id");
}
return id;
}
Expand All @@ -398,8 +398,8 @@ private IdType getVertexIdType() {
for (Field field : declaredFields) {
Annotation[] annotations = field.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof GraphId) {
return ((GraphId) annotation).type();
if (annotation instanceof Id) {
return field.getType() == String.class ? IdType.STRING : IdType.INT64;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import javax.persistence.Transient;
import org.nebula.contrib.ngbatis.annotations.Space;
import org.nebula.contrib.ngbatis.annotations.base.EdgeType;
import org.nebula.contrib.ngbatis.annotations.base.GraphId;
import org.nebula.contrib.ngbatis.annotations.base.Tag;
import org.nebula.contrib.ngbatis.exception.ParseException;
import org.nebula.contrib.ngbatis.models.MethodModel;
Expand Down Expand Up @@ -84,10 +83,6 @@ public static void setValue(Object o, String prop, Object value)
declaredField = columnField;
break;
}
if (isGraphId(columnField) && prop.equals("@GraphId")) {
setValue(o, columnField, value);
return;
}
}
if (declaredField == null) {
throw new NoSuchFieldException(prop);
Expand Down Expand Up @@ -619,7 +614,7 @@ public static String schemaByEntityType(Class<?> entityType) {
public static boolean isGraphId(Field field) {
Annotation[] annotations = field.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof GraphId) {
if (annotation instanceof Id) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ public static void setAttrs(Object t, Node v, String tagName) {
if (!v.tagNames().contains(tagName)) {
return;
}
ReflectUtil.setValue(t, "@GraphId", ResultSetUtil.getValue(v.getId()));
List<ValueWrapper> values = v.values(tagName);
List<String> keys = v.keys(tagName);
for (int i = 0; i < keys.size(); i++) {
Expand Down
Loading