Hibernate and thimeleaf infinite recursion

I have 2 classes with one attribute mapped to the OneToMany / ManyToOne relationship. The problem is that when I make a selection and move on to the view, I want to parse the object in javascript using Thymeleaf, but it loops around the infinite reason for the relationship. My classes: Player class:

@Entity
@Table(name = "player")
public class Player {

@Id
@Column(name = "id")
@GeneratedValue
private int id;

@Column(name = "level")
private int level;

@Column(name = "experience")
private int experience;

@OneToMany(mappedBy="player", cascade = CascadeType.ALL)
private List<InventoryItem> inventory;

// Constructor
public Player() {
}

// Getters & Setters...
}

InventoryItem Class:

@Entity
@Table(name = "inventory_item")
public class InventoryItem {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @ManyToOne
    @JoinColumn(name="id_player")
    private Player player;


    public InventoryItem() {
    }

    //Getters and Setters...
}

Then I pass the Player object to the view and console.log it using javascript:

<script th:inline="javascript">
/*<![CDATA[*/
    console.log([[${player}]]);
/*]]>*/
</script>

And this is a mistake: enter image description here

How can I prevent bidirectional relation to the analysis of javascript, something like atrubute disregard Playerof all InventoryItems?

+4
source share
1 answer

, , Thymeleaf . null, Thymeleaf, .

Thymeleaf, , Introspector, , , a BeanInfo. - bean, BeanInfo, . SimpleBeanInfo ( getPropertyDescriptors).

:

public class InventoryItemBeanInfo extends SimpleBeanInfo {
    @Override
    public PropertyDescriptor[] getPropertyDescriptors() {
        try {  
            PropertyDescriptor id = new PropertyDescriptor("id", InventoryItem.class);         
            PropertyDescriptor[] descriptors = {id};
            return descriptors;
        } catch (IntrospectionException e) {
            throw new Error(e.toString());
        }
    }
}

, Player, .

, , , InventoryItem bean, BeanInfo .

, . , , .

, , . HiddenProperty:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface HiddenProperty {}

BeanInfo , bean . , , :

public class HiddenPropertyAwareBeanInfo extends SimpleBeanInfo {
    private Class<?> beanClass;
    private PropertyDescriptor[] descriptors;

    public HiddenPropertyAwareBeanInfo(Class<?> beanClass) {
        this.beanClass = beanClass;
    }

    @Override
    public PropertyDescriptor[] getPropertyDescriptors() {
        if(descriptors != null) {
            return descriptors;
        }

        Method[] methodList = beanClass.getMethods();
        List<PropertyDescriptor> propDescriptors = new ArrayList<>();

        for(Method m : methodList) {
            if(Modifier.isStatic(m.getModifiers())) {
                continue;
            }

            try {
                if(m.getParameterCount() == 0 && !m.isAnnotationPresent(HiddenProperty.class)) {
                    if(m.getName().startsWith("get")) {
                        propDescriptors.add(new PropertyDescriptor(m.getName().substring(3), beanClass));
                    } else if(m.getName().startsWith("is")) {
                        propDescriptors.add(new PropertyDescriptor(m.getName().substring(2), beanClass));
                    }
                }
            } catch(IntrospectionException ex) {
                continue;
            }
        }

        descriptors = new PropertyDescriptor[propDescriptors.size()];
        return propDescriptors.toArray(descriptors);
    }
}

BeanInfo, :

public class InventoryItemBeanInfo extends HiddenPropertyAwareBeanInfo {
    public InventoryItemBeanInfo() {
        super(InventoryItem.class);
    }
}

, , :

@Entity
@Table(name = "inventory_item")
public class InventoryItem {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;

    @ManyToOne
    @JoinColumn(name="id_player")
    private Player player;


    public InventoryItem() {
    }

    @HiddenProperty
    public Player getPlayer() {
        return this.player;
    }
    //Other getters and setters...
}

. , , Player Thymeleaf.

, , , 100% Java Beans, . Introspector , , .

, , , Thymeleaf. - , .

+4

Source: https://habr.com/ru/post/1584895/


All Articles