diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelAddress.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelAddress.java new file mode 100644 index 0000000000..d5c2f8e7bf --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelAddress.java @@ -0,0 +1,44 @@ +/* + * 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.openjpa.persistence.meta.constants; + +import jakarta.persistence.Embeddable; + +@Embeddable +public class MetamodelAddress { + private String street; + + private String city; + + public String getStreet() { + return street; + } + + public void setStreet(String street) { + this.street = street; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelAddress_.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelAddress_.java new file mode 100644 index 0000000000..504015c6e8 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelAddress_.java @@ -0,0 +1,35 @@ +/* + * 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. + */ +/** + * Generated by OpenJPA MetaModel Generator Tool. +**/ + +package org.apache.openjpa.persistence.meta.constants; + +import jakarta.persistence.metamodel.EmbeddableType; +import jakarta.persistence.metamodel.SingularAttribute; +import java.lang.String; + +@jakarta.persistence.metamodel.StaticMetamodel +(value=org.apache.openjpa.persistence.meta.constants.MetamodelAddress.class) +public class MetamodelAddress_ { + public static volatile SingularAttribute city; + public static volatile EmbeddableType class_; + public static volatile SingularAttribute street; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelBase.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelBase.java new file mode 100644 index 0000000000..c612ceda50 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelBase.java @@ -0,0 +1,46 @@ +/* + * 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.openjpa.persistence.meta.constants; + +import jakarta.persistence.Id; +import jakarta.persistence.MappedSuperclass; + +@MappedSuperclass +public abstract class MetamodelBase { + @Id + private long id; + + private String createdBy; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getCreatedBy() { + return createdBy; + } + + public void setCreatedBy(String createdBy) { + this.createdBy = createdBy; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelBase_.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelBase_.java new file mode 100644 index 0000000000..cca023d57b --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelBase_.java @@ -0,0 +1,36 @@ +/* + * 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. + */ +/** + * Generated by OpenJPA MetaModel Generator Tool. +**/ + +package org.apache.openjpa.persistence.meta.constants; + +import jakarta.persistence.metamodel.MappedSuperclassType; +import jakarta.persistence.metamodel.SingularAttribute; +import java.lang.Long; +import java.lang.String; + +@jakarta.persistence.metamodel.StaticMetamodel +(value=org.apache.openjpa.persistence.meta.constants.MetamodelBase.class) +public class MetamodelBase_ { + public static volatile MappedSuperclassType class_; + public static volatile SingularAttribute createdBy; + public static volatile SingularAttribute id; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelBook.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelBook.java new file mode 100644 index 0000000000..0b9ec5585c --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelBook.java @@ -0,0 +1,34 @@ +/* + * 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.openjpa.persistence.meta.constants; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.NamedEntityGraph; +import jakarta.persistence.NamedQuery; + +@Entity +@NamedQuery(name = "MetamodelBook.all", query = "SELECT b FROM MetamodelBook b") +@NamedEntityGraph(name = "MetamodelBook.graph") +public class MetamodelBook { + @Id + private long id; + + private String title; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelBook_.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelBook_.java new file mode 100644 index 0000000000..902c4fca02 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelBook_.java @@ -0,0 +1,42 @@ +/* + * 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.openjpa.persistence.meta.constants; + +import jakarta.persistence.EntityGraph; +import jakarta.persistence.TypedQueryReference; +import jakarta.persistence.metamodel.EntityType; +import jakarta.persistence.metamodel.SingularAttribute; +import jakarta.persistence.metamodel.StaticMetamodel; + +/** + * Metamodel in the shape of the Hibernate 7 processor output. + */ +@StaticMetamodel(MetamodelBook.class) +public abstract class MetamodelBook_ { + public static final String ID = "id"; + public static final String TITLE = "title"; + public static final String QUERY_METAMODEL_BOOK_ALL = "MetamodelBook.all"; + public static final String GRAPH_METAMODEL_BOOK_GRAPH = "MetamodelBook.graph"; + + public static volatile EntityType class_; + public static volatile SingularAttribute id; + public static volatile SingularAttribute title; + public static volatile TypedQueryReference _MetamodelBook_all_; + public static volatile EntityGraph _MetamodelBook_graph; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelDual.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelDual.java new file mode 100644 index 0000000000..c8f4be34a7 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelDual.java @@ -0,0 +1,51 @@ +/* + * 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.openjpa.persistence.meta.constants; + +import jakarta.persistence.Embeddable; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +/** + * Entity that is also an embeddable. + */ +@Entity +@Embeddable +public class MetamodelDual { + @Id + private long id; + + private String city; + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelDual_.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelDual_.java new file mode 100644 index 0000000000..fee9a4a488 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelDual_.java @@ -0,0 +1,36 @@ +/* + * 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. + */ +/** + * Generated by OpenJPA MetaModel Generator Tool. +**/ + +package org.apache.openjpa.persistence.meta.constants; + +import jakarta.persistence.metamodel.EntityType; +import jakarta.persistence.metamodel.SingularAttribute; +import java.lang.Long; +import java.lang.String; + +@jakarta.persistence.metamodel.StaticMetamodel +(value=org.apache.openjpa.persistence.meta.constants.MetamodelDual.class) +public class MetamodelDual_ { + public static volatile SingularAttribute city; + public static volatile EntityType class_; + public static volatile SingularAttribute id; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelItem.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelItem.java new file mode 100644 index 0000000000..a85ca2fc2e --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelItem.java @@ -0,0 +1,69 @@ +/* + * 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.openjpa.persistence.meta.constants; + +import java.util.ArrayList; +import java.util.List; + +import jakarta.persistence.ColumnResult; +import jakarta.persistence.ElementCollection; +import jakarta.persistence.Embedded; +import jakarta.persistence.Entity; +import jakarta.persistence.NamedNativeQuery; +import jakarta.persistence.NamedQueries; +import jakarta.persistence.NamedQuery; +import jakarta.persistence.SqlResultSetMapping; + +@Entity +@NamedQueries({ + @NamedQuery(name = "MetamodelItem.byName", query = "SELECT i FROM MetamodelItem i WHERE i.name = :name"), + @NamedQuery(name = "MetamodelItem.all", query = "SELECT i FROM MetamodelItem i") +}) +@NamedNativeQuery(name = "MetamodelItem.native", query = "SELECT NAME FROM MetamodelItem", + resultSetMapping = "MetamodelItem.names") +@SqlResultSetMapping(name = "MetamodelItem.names", columns = @ColumnResult(name = "NAME")) +public class MetamodelItem extends MetamodelBase { + private String name; + + @Embedded + private MetamodelAddress address; + + @ElementCollection + private List tags = new ArrayList<>(); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public MetamodelAddress getAddress() { + return address; + } + + public void setAddress(MetamodelAddress address) { + this.address = address; + } + + public List getTags() { + return tags; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelItem_.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelItem_.java new file mode 100644 index 0000000000..2a1372c45b --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelItem_.java @@ -0,0 +1,40 @@ +/* + * 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. + */ +/** + * Generated by OpenJPA MetaModel Generator Tool. +**/ + +package org.apache.openjpa.persistence.meta.constants; + +import jakarta.persistence.metamodel.EntityType; +import jakarta.persistence.metamodel.ListAttribute; +import jakarta.persistence.metamodel.SingularAttribute; + +@jakarta.persistence.metamodel.StaticMetamodel +(value=org.apache.openjpa.persistence.meta.constants.MetamodelItem.class) +public class MetamodelItem_ extends MetamodelBase_ { + public static final String MAPPING_METAMODELITEM_NAMES = "MetamodelItem.names"; + public static final String QUERY_METAMODELITEM_ALL = "MetamodelItem.all"; + public static final String QUERY_METAMODELITEM_BYNAME = "MetamodelItem.byName"; + public static final String QUERY_METAMODELITEM_NATIVE = "MetamodelItem.native"; + public static volatile SingularAttribute address; + public static volatile EntityType class_; + public static volatile SingularAttribute name; + public static volatile ListAttribute tags; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelMismatch.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelMismatch.java new file mode 100644 index 0000000000..55c91e4b3e --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelMismatch.java @@ -0,0 +1,30 @@ +/* + * 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.openjpa.persistence.meta.constants; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +@Entity +public class MetamodelMismatch { + @Id + private long id; + + private String name; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelMismatch_.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelMismatch_.java new file mode 100644 index 0000000000..9d712cded8 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelMismatch_.java @@ -0,0 +1,33 @@ +/* + * 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.openjpa.persistence.meta.constants; + +import jakarta.persistence.metamodel.EntityType; +import jakarta.persistence.metamodel.SingularAttribute; + +/** + * Canonical metamodel with a field for an attribute the entity does not have. + */ +@jakarta.persistence.metamodel.StaticMetamodel +(value=org.apache.openjpa.persistence.meta.constants.MetamodelMismatch.class) +public class MetamodelMismatch_ { + public static volatile EntityType class_; + public static volatile SingularAttribute id; + public static volatile SingularAttribute unknown; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelPart.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelPart.java new file mode 100644 index 0000000000..60aa1ebfd5 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelPart.java @@ -0,0 +1,26 @@ +/* + * 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.openjpa.persistence.meta.constants; + +import jakarta.persistence.Embeddable; + +@Embeddable +public class MetamodelPart { + private String label; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelPart_.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelPart_.java new file mode 100644 index 0000000000..c5e68a74b6 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelPart_.java @@ -0,0 +1,29 @@ +/* + * 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.openjpa.persistence.meta.constants; + +import jakarta.persistence.metamodel.ManagedType; +import jakarta.persistence.metamodel.SingularAttribute; +import jakarta.persistence.metamodel.StaticMetamodel; + +@StaticMetamodel(MetamodelPart.class) +public class MetamodelPart_ { + public static volatile ManagedType class_; + public static volatile SingularAttribute label; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelWildcard.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelWildcard.java new file mode 100644 index 0000000000..6d6dcefd0e --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelWildcard.java @@ -0,0 +1,28 @@ +/* + * 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.openjpa.persistence.meta.constants; + +import jakarta.persistence.Id; +import jakarta.persistence.MappedSuperclass; + +@MappedSuperclass +public class MetamodelWildcard { + @Id + private long id; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelWildcard_.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelWildcard_.java new file mode 100644 index 0000000000..4a808b4cf6 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/MetamodelWildcard_.java @@ -0,0 +1,29 @@ +/* + * 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.openjpa.persistence.meta.constants; + +import jakarta.persistence.metamodel.EntityType; +import jakarta.persistence.metamodel.SingularAttribute; +import jakarta.persistence.metamodel.StaticMetamodel; + +@StaticMetamodel(MetamodelWildcard.class) +public class MetamodelWildcard_ { + public static volatile EntityType class_; + public static volatile SingularAttribute id; +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/TestHandWrittenMetamodel.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/TestHandWrittenMetamodel.java new file mode 100644 index 0000000000..fdab3a6c2b --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/TestHandWrittenMetamodel.java @@ -0,0 +1,56 @@ +/* + * 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.openjpa.persistence.meta.constants; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; + +import jakarta.persistence.metamodel.Metamodel; + +import org.apache.openjpa.persistence.test.SingleEMFTestCase; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Metamodel classes not written by the OpenJPA annotation processor. + */ +class TestHandWrittenMetamodel extends SingleEMFTestCase { + @BeforeEach + public void setUp() { + setUp(MetamodelBook.class, MetamodelPart.class); + } + + @Test + void fieldsOtherThanAttributesAreSkipped() { + Metamodel mm = emf.getMetamodel(); + assertSame(mm.entity(MetamodelBook.class), MetamodelBook_.class_); + assertEquals("title", MetamodelBook_.title.getName()); + assertEquals("MetamodelBook.all", MetamodelBook_.QUERY_METAMODEL_BOOK_ALL); + assertNull(MetamodelBook_._MetamodelBook_all_); + assertNull(MetamodelBook_._MetamodelBook_graph); + } + + @Test + void managedTypeOfEmbeddableIsEmbeddableType() { + Metamodel mm = emf.getMetamodel(); + assertSame(mm.embeddable(MetamodelPart.class), MetamodelPart_.class_); + assertEquals("label", MetamodelPart_.label.getName()); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/TestMetamodelClassConstant.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/TestMetamodelClassConstant.java new file mode 100644 index 0000000000..6df48ff436 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/TestMetamodelClassConstant.java @@ -0,0 +1,217 @@ +/* + * 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.openjpa.persistence.meta.constants; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +import javax.tools.DiagnosticCollector; +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.ToolProvider; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.TypedQuery; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Root; +import jakarta.persistence.metamodel.EmbeddableType; +import jakarta.persistence.metamodel.EntityType; +import jakarta.persistence.metamodel.MappedSuperclassType; +import jakarta.persistence.metamodel.Metamodel; +import jakarta.persistence.metamodel.Type.PersistenceType; + +import org.apache.openjpa.persistence.meta.MetamodelImpl; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +/** + * Populates a canonical metamodel with the JPA 3.2 class_ and String constants + * generated by the metamodel annotation processor. + */ +class TestMetamodelClassConstant extends SingleEMFTestCase { + private static final String[] TYPES = { "MetamodelBase", "MetamodelItem", "MetamodelAddress", "MetamodelDual" }; + + @TempDir + Path tmp; + + @BeforeEach + public void setUp() { + setUp(CLEAR_TABLES, MetamodelBase.class, MetamodelItem.class, MetamodelAddress.class, + MetamodelDual.class); + } + + @Test + void criteriaBuilderPopulatesMetamodel() { + assertNotNull(emf.getCriteriaBuilder()); + assertNotNull(MetamodelItem_.class_); + assertNotNull(MetamodelItem_.name); + } + + @Test + void classConstantIsManagedType() { + Metamodel mm = emf.getMetamodel(); + EntityType item = mm.entity(MetamodelItem.class); + assertSame(item, MetamodelItem_.class_); + + MappedSuperclassType base = assertInstanceOf(MappedSuperclassType.class, + mm.managedType(MetamodelBase.class)); + assertSame(base, MetamodelBase_.class_); + + EmbeddableType address = mm.embeddable(MetamodelAddress.class); + assertSame(address, MetamodelAddress_.class_); + } + + @Test + void classConstantOfEntityThatIsEmbeddable() { + MetamodelImpl mm = (MetamodelImpl) emf.getMetamodel(); + assertSame(mm.entityImpl(MetamodelDual.class), MetamodelDual_.class_); + assertEquals(PersistenceType.ENTITY, MetamodelDual_.class_.getPersistenceType()); + assertEquals("city", MetamodelDual_.city.getName()); + } + + @Test + void attributesArePopulated() { + Metamodel mm = emf.getMetamodel(); + EntityType item = mm.entity(MetamodelItem.class); + assertSame(item.getAttribute("name"), MetamodelItem_.name); + assertSame(item.getAttribute("tags"), MetamodelItem_.tags); + assertSame(item.getAttribute("address"), MetamodelItem_.address); + assertEquals("id", MetamodelBase_.id.getName()); + assertEquals("createdBy", MetamodelBase_.createdBy.getName()); + assertEquals("city", MetamodelAddress_.city.getName()); + assertEquals("street", MetamodelAddress_.street.getName()); + } + + @Test + void stringConstantsAreKept() { + emf.getMetamodel(); + assertEquals("MetamodelItem.byName", MetamodelItem_.QUERY_METAMODELITEM_BYNAME); + assertEquals("MetamodelItem.all", MetamodelItem_.QUERY_METAMODELITEM_ALL); + assertEquals("MetamodelItem.native", MetamodelItem_.QUERY_METAMODELITEM_NATIVE); + assertEquals("MetamodelItem.names", MetamodelItem_.MAPPING_METAMODELITEM_NAMES); + } + + @Test + void criteriaQueryUsesMetamodel() { + persist(1, "a", "Berlin"); + persist(2, "b", "Paris"); + + try (EntityManager em = emf.createEntityManager()) { + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery q = cb.createQuery(MetamodelItem.class); + Root root = q.from(MetamodelItem_.class_); + q.where(cb.equal(root.get(MetamodelItem_.address).get(MetamodelAddress_.city), "Paris"), + cb.isNotNull(root.get(MetamodelBase_.createdBy))); + List result = em.createQuery(q).getResultList(); + assertEquals(1, result.size()); + assertEquals(2, result.get(0).getId()); + assertEquals("b", result.get(0).getName()); + + TypedQuery named = em.createNamedQuery(MetamodelItem_.QUERY_METAMODELITEM_BYNAME, + MetamodelItem.class); + named.setParameter("name", "a"); + assertEquals(1, named.getResultList().size()); + } + } + + @Test + void metamodelMatchesProcessorOutput() throws IOException { + Path sources = Paths.get("src", "test", "java", "org", "apache", "openjpa", "persistence", "meta", + "constants"); + Path generated = tmp.resolve("generated"); + Path classes = tmp.resolve("classes"); + Files.createDirectories(generated); + Files.createDirectories(classes); + + List options = new ArrayList<>(); + options.add("-classpath"); + options.add(System.getProperty("java.class.path")); + options.add("-processorpath"); + options.add(System.getProperty("java.class.path")); + options.add("-d"); + options.add(classes.toString()); + options.add("-s"); + options.add(generated.toString()); + options.add("-Aopenjpa.metamodel=true"); + + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + DiagnosticCollector diagnostics = new DiagnosticCollector<>(); + try (StandardJavaFileManager fm = compiler.getStandardFileManager(diagnostics, null, + StandardCharsets.UTF_8)) { + List files = new ArrayList<>(); + for (String type : TYPES) { + files.add(sources.resolve(type + ".java").toFile()); + } + Iterable units = fm.getJavaFileObjectsFromFiles(files); + boolean ok = compiler.getTask(null, fm, diagnostics, options, null, units).call(); + assertTrue(ok, "compilation failed: " + diagnostics.getDiagnostics()); + } + + Path pkg = Paths.get("org", "apache", "openjpa", "persistence", "meta", "constants"); + for (String type : TYPES) { + Path out = generated.resolve(pkg).resolve(type + "_.java"); + assertTrue(Files.exists(out), "metamodel class was not generated: " + out); + assertEquals(body(sources.resolve(type + "_.java")), body(out), type + "_ differs from processor output"); + } + } + + /** + * Source from the package declaration on, without trailing whitespace. + */ + private static String body(Path source) throws IOException { + String text = new String(Files.readAllBytes(source), StandardCharsets.UTF_8); + return text.substring(text.indexOf("package ")).lines() + .map(String::stripTrailing) + .collect(Collectors.joining("\n")); + } + + private void persist(long id, String name, String city) { + MetamodelAddress address = new MetamodelAddress(); + address.setCity(city); + address.setStreet("Main"); + MetamodelItem item = new MetamodelItem(); + item.setId(id); + item.setName(name); + item.setCreatedBy("test"); + item.setAddress(address); + item.getTags().add("t" + id); + try (EntityManager em = emf.createEntityManager()) { + em.getTransaction().begin(); + em.persist(item); + em.getTransaction().commit(); + } + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/TestMetamodelClassFieldMismatch.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/TestMetamodelClassFieldMismatch.java new file mode 100644 index 0000000000..c21a791e79 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/TestMetamodelClassFieldMismatch.java @@ -0,0 +1,41 @@ +/* + * 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.openjpa.persistence.meta.constants; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.apache.openjpa.persistence.test.SingleEMFTestCase; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class TestMetamodelClassFieldMismatch extends SingleEMFTestCase { + @BeforeEach + public void setUp() { + setUp(MetamodelWildcard.class); + } + + @Test + void wrongCategoryIsReported() { + IllegalStateException e = assertThrows(IllegalStateException.class, () -> emf.getMetamodel()); + assertTrue(e.getMessage().contains("\"class_\""), e.getMessage()); + assertTrue(e.getMessage().contains("EntityType"), e.getMessage()); + assertTrue(e.getMessage().contains("MAPPED_SUPERCLASS"), e.getMessage()); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/TestMetamodelFieldMismatch.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/TestMetamodelFieldMismatch.java new file mode 100644 index 0000000000..b8164e93f5 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/TestMetamodelFieldMismatch.java @@ -0,0 +1,49 @@ +/* + * 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.openjpa.persistence.meta.constants; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.apache.openjpa.persistence.test.SingleEMFTestCase; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Reports a canonical metamodel field that does not match a persistent attribute. + */ +class TestMetamodelFieldMismatch extends SingleEMFTestCase { + @BeforeEach + public void setUp() { + setUp(MetamodelMismatch.class); + } + + @Test + void unknownAttributeIsReported() { + RuntimeException e = assertThrows(RuntimeException.class, () -> emf.getMetamodel()); + assertTrue(e.getMessage().contains("\"unknown\""), e.getMessage()); + assertTrue(e.getMessage().contains("SingularAttribute<"), e.getMessage()); + assertTrue(e.getMessage().contains(",?>"), e.getMessage()); + assertFalse(e.getMessage().contains("java.lang.Long"), e.getMessage()); + assertTrue(e.getCause() instanceof IllegalArgumentException, String.valueOf(e.getCause())); + assertSame(MetamodelMismatch.class, MetamodelMismatch_.class_.getJavaType()); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/TestMetamodelProcessorConstants.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/TestMetamodelProcessorConstants.java new file mode 100644 index 0000000000..a0439c2a65 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/meta/constants/TestMetamodelProcessorConstants.java @@ -0,0 +1,110 @@ +/* + * 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.openjpa.persistence.meta.constants; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + +import javax.tools.DiagnosticCollector; +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.ToolProvider; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +/** + * Named query constants generated by the annotation processor. + */ +class TestMetamodelProcessorConstants { + private static final String SOURCE = String.join("\n", + "package p;", + "import jakarta.persistence.*;", + "@Entity", + "@NamedQueries({", + " @NamedQuery(name = \"Coll.byName\", query = \"SELECT c FROM Coll c\"),", + " @NamedQuery(name = \"Coll.byname\", query = \"SELECT c FROM Coll c\"),", + " @NamedQuery(name = \"Quote \\\"q\\\" \\\\ x\\t\", query = \"SELECT c FROM Coll c\")", + "})", + "public class Coll {", + " @Id private long id;", + "}"); + + @TempDir + Path tmp; + + @Test + void collidingAndQuotedQueryNames() throws Exception { + Path src = tmp.resolve("src"); + Path generated = tmp.resolve("generated"); + Path classes = tmp.resolve("classes"); + Files.createDirectories(src.resolve("p")); + Files.createDirectories(generated); + Files.createDirectories(classes); + Path entity = src.resolve("p").resolve("Coll.java"); + Files.write(entity, SOURCE.getBytes(StandardCharsets.UTF_8)); + + List options = new ArrayList<>(); + options.add("-classpath"); + options.add(System.getProperty("java.class.path")); + options.add("-processorpath"); + options.add(System.getProperty("java.class.path")); + options.add("-d"); + options.add(classes.toString()); + options.add("-s"); + options.add(generated.toString()); + options.add("-Aopenjpa.metamodel=true"); + + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + DiagnosticCollector diagnostics = new DiagnosticCollector<>(); + try (StandardJavaFileManager fm = compiler.getStandardFileManager(diagnostics, null, + StandardCharsets.UTF_8)) { + boolean ok = compiler.getTask(null, fm, diagnostics, options, null, + fm.getJavaFileObjects(entity.toFile())).call(); + assertTrue(ok, "compilation failed: " + diagnostics.getDiagnostics()); + } + + List values = new ArrayList<>(); + try (URLClassLoader loader = new URLClassLoader(new URL[] { classes.toUri().toURL() }, + getClass().getClassLoader())) { + for (Field f : loader.loadClass("p.Coll_").getDeclaredFields()) { + if (Modifier.isFinal(f.getModifiers()) && f.getType() == String.class) { + values.add((String) f.get(null)); + } + } + } catch (IOException e) { + throw new AssertionError(e); + } + assertEquals(2, values.size(), values.toString()); + assertTrue(values.contains("Coll.byName"), values.toString()); + assertTrue(values.contains("Quote \"q\" \\ x\t"), values.toString()); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestMetamodelConstants.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestMetamodelConstants.java index 72987d99c9..4a6d2db454 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestMetamodelConstants.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/simple/TestMetamodelConstants.java @@ -19,6 +19,7 @@ package org.apache.openjpa.persistence.simple; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Field; @@ -80,6 +81,15 @@ void entityTypeFieldExists() throws Exception { assertEquals(EntityType.class, f.getType()); } + @Test + void entityTypeFieldIsPopulated() { + EntityType type = emf.getMetamodel().entity(MetamodelConstantsEntity.class); + assertSame(type, MetamodelConstantsEntity_.class_); + assertSame(type.getAttribute("name"), MetamodelConstantsEntity_.name); + assertEquals("MetamodelConstantsEntity.findAll", + MetamodelConstantsEntity_.QUERY_METAMODELCONSTANTSENTITY_FINDALL); + } + @Test void queryConstantsUsableWithCreateNamedQuery() { EntityManager em = emf.createEntityManager(); diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java index c42bd33b70..e5686e96e6 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/AnnotationProcessor6.java @@ -377,10 +377,7 @@ private void addNamedQueryConstants(TypeElement e, SourceCode.Class modelClass) } private void addQueryConstant(SourceCode.Class modelClass, String queryName) { - String constName = "QUERY_" + toConstantName(queryName); - SourceCode.Field f = modelClass.addField(constName, "String"); - f.setInitialValue("\"" + queryName + "\""); - f.makePublic().makeStatic().makeFinal(); + addStringConstant(modelClass, "QUERY_" + toConstantName(queryName), queryName); } private void addResultSetMappingConstants(TypeElement e, SourceCode.Class modelClass) { @@ -397,12 +394,39 @@ private void addResultSetMappingConstants(TypeElement e, SourceCode.Class modelC } private void addMappingConstant(SourceCode.Class modelClass, String mappingName) { - String constName = "MAPPING_" + toConstantName(mappingName); - SourceCode.Field f = modelClass.addField(constName, "String"); - f.setInitialValue("\"" + mappingName + "\""); + addStringConstant(modelClass, "MAPPING_" + toConstantName(mappingName), mappingName); + } + + /** + * Adds a String constant, skipping one whose name is already taken. + */ + private void addStringConstant(SourceCode.Class modelClass, String constName, String value) { + SourceCode.Field f; + try { + f = modelClass.addField(constName, "String"); + } catch (IllegalArgumentException e) { + logger.warn(_loc.get("mmg-duplicate-constant", constName, value)); + return; + } + f.setInitialValue(toStringLiteral(value)); f.makePublic().makeStatic().makeFinal(); } + private static String toStringLiteral(String value) { + StringBuilder sb = new StringBuilder("\""); + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + if (c == '"' || c == '\\') { + sb.append('\\').append(c); + } else if (c < ' ') { + sb.append(String.format("\\%03o", (int) c)); + } else { + sb.append(c); + } + } + return sb.append('"').toString(); + } + private static String toConstantName(String name) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < name.length(); i++) { diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/MetamodelImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/MetamodelImpl.java index 0ff340d75d..6733f38884 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/MetamodelImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/meta/MetamodelImpl.java @@ -25,6 +25,7 @@ import java.lang.reflect.Field; import java.lang.reflect.GenericArrayType; +import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.util.Collection; import java.util.HashMap; @@ -75,6 +76,11 @@ public class MetamodelImpl implements Metamodel, Resolver { private static Localizer _loc = Localizer.forPackage(MetamodelImpl.class); + /** + * Name of the JPA 3.2 metamodel field that holds the managed type itself. + */ + private static final String MANAGED_TYPE_FIELD = "class_"; + /** * Constructs a model with the current content of the supplied non-null repository. * @@ -379,10 +385,27 @@ public void populate(AbstractManagedType type) { mcls.getName(), cls.getName(), anno.value()).getMessage()); } - ParameterizedType mfType = null; - Attribute f = null; - Field[] mfields = mcls.getDeclaredFields(); - for (Field mf : mfields) { + for (Field mf : mcls.getDeclaredFields()) { + int mod = mf.getModifiers(); + if (Modifier.isStatic(mod) && Modifier.isFinal(mod)) { + // JPA 3.2 String constants such as QUERY_ and MAPPING_ + continue; + } + if (MANAGED_TYPE_FIELD.equals(mf.getName()) && ManagedType.class.isAssignableFrom(mf.getType())) { + populateManagedTypeField(mf, type); + continue; + } + if (!Attribute.class.isAssignableFrom(mf.getType())) { + // such as TypedQueryReference or EntityGraph of other generators + Log log = repos.getLog(); + if (log.isTraceEnabled()) { + log.trace(_loc.get("meta-field-not-param", mcls, mf.getName(), + toTypeName(mf.getGenericType())).getMessage()); + } + continue; + } + ParameterizedType mfType = null; + Attribute f = null; try { mfType = getParameterizedType(mf); // metamodel type if (mfType == null) { @@ -394,12 +417,43 @@ public void populate(AbstractManagedType type) { mf.set(null, f); } catch (Exception e) { throw new RuntimeException(_loc.get("meta-field-mismatch", - new Object[] { mf.getName(), mcls.getName(), toTypeName(mfType), f.getJavaType().toString() }) + new Object[] { mf.getName(), mcls.getName(), + mfType == null ? toTypeName(mf.getGenericType()) : toTypeName(mfType), + f == null ? "none" : f.getJavaType().getName() }) .getMessage(), e); } } } + /** + * Sets the JPA 3.2 class_ field of a canonical metamodel class to the given managed type. + * An embeddable is populated as embeddable and then as entity, the embeddable type is + * preferred if the field can hold it. + */ + private void populateManagedTypeField(Field mf, AbstractManagedType type) { + Class fieldType = mf.getType(); + EmbeddableType embeddable = _embeddables.get(type.getJavaType()); + ManagedType value = fieldType.isInstance(embeddable) ? embeddable : type; + if (!fieldType.isInstance(value)) { + if (value == embeddable) { + // the entity pass follows + return; + } + throw new IllegalStateException(managedTypeFieldMismatch(mf, type)); + } + try { + mf.set(null, value); + } catch (IllegalAccessException e) { + throw new IllegalStateException(managedTypeFieldMismatch(mf, type), e); + } + } + + private String managedTypeFieldMismatch(Field mf, AbstractManagedType type) { + return _loc.get("meta-class-field-mismatch", new Object[] { mf.getName(), + mf.getDeclaringClass().getName(), toTypeName(mf.getGenericType()), type.getPersistenceType() }) + .getMessage(); + } + /** * Gets the parameterized type of the given field after validating. * @@ -433,10 +487,12 @@ String toTypeName(java.lang.reflect.Type type) { return toTypeName(((GenericArrayType)type). getGenericComponentType())+"[]"; } - if (!(type instanceof ParameterizedType)) { - Class cls = (Class)type; + if (type instanceof Class cls) { return cls.getName(); } + if (!(type instanceof ParameterizedType)) { + return type.getTypeName(); + } ParameterizedType pType = (ParameterizedType)type; java.lang.reflect.Type[] args = pType.getActualTypeArguments(); StringBuilder tmp = new StringBuilder(pType.getRawType().toString()); diff --git a/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/meta/localizer.properties b/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/meta/localizer.properties index f959b00023..9c0e7c9af8 100644 --- a/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/meta/localizer.properties +++ b/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/meta/localizer.properties @@ -42,6 +42,7 @@ mmg-tool-banner: Starting OpenJPA Annotation Processor for Metamodel Generation mmg-process: Generating canonical metamodel source code "{0}" mmg-process-error: Error while generating metamodel for "{0}". See exception \ stack trace for details. +mmg-duplicate-constant: Skipping constant "{0}" for "{1}", its name is already taken. mmg-no-type: Type {0} not found. This may happen if {0} is not compiled. mmg-tool-sign: Generated by OpenJPA MetaModel Generator Tool. mmg-bad-source: Wrong value "{0}" of -Asource option to specify the target \ @@ -58,6 +59,8 @@ meta-class-mismatch: Value provided for the StaticMetamodel annotation "{2}" \ in Meta class "{0}" does not match the expected Entity class "{1}". meta-field-mismatch: The field "{0}" in meta class "{1}" is of type "{2}" \ which does not support the persistent property type "{3}". +meta-class-field-mismatch: The field "{0}" in meta class "{1}" is of type "{2}" \ + which can not hold a managed type of category "{3}". meta-field-not-param: Encountered field "{2} {1}" in "{0}". This field is \ not recognized as canonical field. Ignoring. meta-field-less-param: Encountered field "{2} {1}" in "{0}". This field has \