Skip to content

Commit

Permalink
Enable composer for embeddable
Browse files Browse the repository at this point in the history
  • Loading branch information
hugothomas committed Dec 12, 2023
1 parent 7f0f842 commit 75b44bf
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
Expand Down Expand Up @@ -43,6 +44,8 @@ public class Product {
@Access(value = AccessType.PROPERTY)
private long id = 0;

@Embedded private Tracking tracking;

private String name;

private String barcode;
Expand Down Expand Up @@ -75,6 +78,14 @@ public void setId(long id) {
this.id = id;
}

public Tracking getTracking() {
return tracking;
}

public void setTracking(Tracking tracking) {
this.tracking = tracking;
}

public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.cosium.spring.data.jpa.entity.graph.sample;

import jakarta.persistence.Embeddable;
import jakarta.persistence.FetchType;
import jakarta.persistence.ManyToOne;

@Embeddable
public class Tracking {
@ManyToOne(fetch = FetchType.LAZY)
private User modifier;

@ManyToOne(fetch = FetchType.LAZY)
private User creator;

public User getModifier() {
return modifier;
}

public void setModifier(User modifier) {
this.modifier = modifier;
}

public User getCreator() {
return creator;
}

public void setCreator(User creator) {
this.creator = creator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.cosium.spring.data.jpa.entity.graph.sample;

import jakarta.persistence.Access;
import jakarta.persistence.AccessType;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Access(value = AccessType.PROPERTY)
private long id = 0;

private String login;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getLogin() {
return login;
}

public void setLogin(String login) {
this.login = login;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@

<category id="1" name="Category 1" />

<product id="1" name="Product 1" brand_id="1" maker_id="1" category_id="1" barcode="1111"/>
<product id="2" name="Product 2" brand_id="1" maker_id="1" category_id="1" barcode="2222"/>
<product id="3" name="Product 3" brand_id="2" maker_id="1" category_id="1" barcode="3333"/>
<product id="4" name="Product 4" brand_id="2" maker_id="1" category_id="1" barcode="4444"/>

<product id="5" name="Product 5" brand_id="1" maker_id="1" category_id="1" barcode="5555"/>
<product id="6" name="Product 6" brand_id="1" maker_id="1" category_id="1" barcode="6666"/>
<product id="7" name="Product 7" brand_id="2" maker_id="1" category_id="1" barcode="7777"/>
<product id="8" name="Product 8" brand_id="2" maker_id="1" category_id="1" barcode="8888"/>

<product id="9" name="Product 9" brand_id="1" maker_id="1" category_id="1" barcode="9999"/>
<product id="10" name="Product 10" brand_id="1" maker_id="1" category_id="1" barcode="10101010"/>
<product id="11" name="Product 11" brand_id="2" maker_id="1" category_id="1" barcode="11111111"/>
<product id="12" name="Product 12" brand_id="2" maker_id="1" category_id="1" barcode="12121212"/>
<user id="1" login="toto"/>

<product id="1" name="Product 1" brand_id="1" maker_id="1" category_id="1" barcode="1111" creator_id="1" modifier_id="1"/>
<product id="2" name="Product 2" brand_id="1" maker_id="1" category_id="1" barcode="2222" creator_id="1" modifier_id="1"/>
<product id="3" name="Product 3" brand_id="2" maker_id="1" category_id="1" barcode="3333" creator_id="1" modifier_id="1"/>
<product id="4" name="Product 4" brand_id="2" maker_id="1" category_id="1" barcode="4444" creator_id="1" modifier_id="1"/>

<product id="5" name="Product 5" brand_id="1" maker_id="1" category_id="1" barcode="5555" creator_id="1" modifier_id="1"/>
<product id="6" name="Product 6" brand_id="1" maker_id="1" category_id="1" barcode="6666" creator_id="1" modifier_id="1"/>
<product id="7" name="Product 7" brand_id="2" maker_id="1" category_id="1" barcode="7777" creator_id="1" modifier_id="1"/>
<product id="8" name="Product 8" brand_id="2" maker_id="1" category_id="1" barcode="8888" creator_id="1" modifier_id="1"/>

<product id="9" name="Product 9" brand_id="1" maker_id="1" category_id="1" barcode="9999" creator_id="1" modifier_id="1"/>
<product id="10" name="Product 10" brand_id="1" maker_id="1" category_id="1" barcode="10101010" creator_id="1" modifier_id="1"/>
<product id="11" name="Product 11" brand_id="2" maker_id="1" category_id="1" barcode="11111111" creator_id="1" modifier_id="1"/>
<product id="12" name="Product 12" brand_id="2" maker_id="1" category_id="1" barcode="12121212" creator_id="1" modifier_id="1"/>

<defaultentitygraphtest_entity id="1" maker_id="1"/>
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static javax.lang.model.element.ElementKind.*;

import jakarta.persistence.Embeddable;
import jakarta.persistence.Entity;
import jakarta.persistence.metamodel.Attribute;
import jakarta.persistence.metamodel.PluralAttribute;
Expand Down Expand Up @@ -74,7 +75,9 @@ public Optional<MetamodelAttributeTarget> jpaTarget() {
return Optional.empty();
}

if (targetTypeElement.getAnnotation(Entity.class) == null && !pluralAttribute) {
if ((targetTypeElement.getAnnotation(Entity.class) == null
&& targetTypeElement.getAnnotation(Embeddable.class) == null)
&& !pluralAttribute) {
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;

import jakarta.persistence.Embeddable;
import jakarta.persistence.Entity;
import javax.lang.model.element.TypeElement;

Expand All @@ -13,11 +14,13 @@ public class MetamodelAttributeTarget {
private final String attributeName;
private final TypeElement targetType;
private final Entity entityAnnotation;
private final Embeddable embeddableAnnotation;

public MetamodelAttributeTarget(String attributeName, TypeElement targetType) {
this.attributeName = requireNonNull(attributeName);
this.targetType = requireNonNull(targetType);
this.entityAnnotation = targetType.getAnnotation(Entity.class);
this.embeddableAnnotation = targetType.getAnnotation(Embeddable.class);
}

public String attributeName() {
Expand All @@ -31,4 +34,8 @@ public TypeElement targetType() {
public boolean isEntity() {
return entityAnnotation != null;
}

public boolean isEmbeddable() {
return embeddableAnnotation != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public TypeSpec toTypeSpec() {

@Override
public void addPath(Elements elements, MetamodelAttributeTarget target) {
if (target.isEntity()) {
if (target.isEntity() || target.isEmbeddable()) {
addPathToEntity(elements, target);
} else {
referencesLeafComposer = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public TypeSpec toTypeSpec() {

@Override
public void addPath(Elements elements, MetamodelAttributeTarget target) {
if (target.isEntity()) {
if (target.isEntity() || target.isEmbeddable()) {
addPathToEntity(elements, target);
} else {
referencesLeafComposer = true;
Expand Down

0 comments on commit 75b44bf

Please sign in to comment.