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

hibernate: remove entity and queries from hbm.xml and add this info as annotation #9563

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion java/buildconf/test/rhn.conf.postgresql-example
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
hibernate.connection.driver_class = org.postgresql.Driver
hibernate.connection.driver_proto = jdbc:postgresql
# Uncomment to log all SQL statements issued by Hibernate
# hibernate.show_sql = true
hibernate.show_sql = true
hibernate.use_outer_join = true
hibernate.jdbc.batch_size = 0
hibernate.cache.region.factory_class = org.hibernate.cache.ehcache.EhCacheRegionFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1301,29 +1301,6 @@ ORDER BY PN.name, PE.evr
</query>
</callable-mode>

<write-mode name="add_servers_to_server_group">
<query params="sgid">
INSERT INTO rhnServerGroupMembers(server_id, server_group_id)
SELECT S.id server_id, :sgid
FROM rhnServerGroup SG, rhnServer S
WHERE S.id IN (%s)
AND SG.id = :sgid
AND S.org_id = SG.org_id
AND NOT EXISTS (SELECT 1
FROM rhnServerGroupMembers SGM
WHERE SGM.server_id = S.id
AND SGM.server_group_id = SG.id)
</query>
</write-mode>

<write-mode name="update_current_members_of_server_group">
<query params="sgid, members_count">
UPDATE rhnServerGroup
SET current_members = current_members + (:members_count)
WHERE id = :sgid
</query>
</write-mode>

<write-mode name="delete_from_servergroup">
<query params="sgid">
DELETE FROM rhnServerGroupMembers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ protected void createSessionFactory() {

sessionFactory = config.buildSessionFactory();
}
catch (HibernateException e) {
catch (Exception e) {
LOG.error("FATAL ERROR creating HibernateFactory", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@
*/
package com.redhat.rhn.common.hibernate;

import com.redhat.rhn.domain.channel.AccessToken;
import com.redhat.rhn.domain.channel.AppStream;
import com.redhat.rhn.domain.channel.AppStreamApi;
import com.redhat.rhn.domain.channel.AppStreamApiKey;
import com.redhat.rhn.domain.channel.Channel;
import com.redhat.rhn.domain.channel.ChannelArch;
import com.redhat.rhn.domain.channel.ChannelSyncFlag;
import com.redhat.rhn.domain.channel.ClonedChannel;
import com.redhat.rhn.domain.cloudpayg.CloudRmtHost;
import com.redhat.rhn.domain.cloudpayg.PaygCredentialsProduct;
import com.redhat.rhn.domain.cloudpayg.PaygSshData;
import com.redhat.rhn.domain.common.ProvisionState;
import com.redhat.rhn.domain.config.ConfigChannel;
import com.redhat.rhn.domain.config.ConfigChannelType;
import com.redhat.rhn.domain.config.ConfigFile;
import com.redhat.rhn.domain.contentmgmt.ContentEnvironment;
import com.redhat.rhn.domain.contentmgmt.ContentFilter;
import com.redhat.rhn.domain.contentmgmt.ContentProject;
Expand Down Expand Up @@ -53,8 +62,18 @@
import com.redhat.rhn.domain.image.ImageStoreType;
import com.redhat.rhn.domain.image.KiwiProfile;
import com.redhat.rhn.domain.image.ProfileCustomDataValue;
import com.redhat.rhn.domain.kickstart.crypto.CryptoKey;
import com.redhat.rhn.domain.kickstart.crypto.CryptoKeyType;
import com.redhat.rhn.domain.kickstart.crypto.SslCryptoKey;
import com.redhat.rhn.domain.notification.NotificationMessage;
import com.redhat.rhn.domain.notification.UserNotification;
import com.redhat.rhn.domain.org.Org;
import com.redhat.rhn.domain.org.OrgAdminManagement;
import com.redhat.rhn.domain.org.OrgConfig;
import com.redhat.rhn.domain.org.TemplateString;
import com.redhat.rhn.domain.org.usergroup.UserGroupImpl;
import com.redhat.rhn.domain.org.usergroup.UserGroupMembers;
import com.redhat.rhn.domain.org.usergroup.UserGroupMembersId;
import com.redhat.rhn.domain.product.SUSEProductSCCRepository;
import com.redhat.rhn.domain.recurringactions.GroupRecurringAction;
import com.redhat.rhn.domain.recurringactions.MinionRecurringAction;
Expand All @@ -75,6 +94,7 @@
import com.redhat.rhn.domain.rhnpackage.PackageRequires;
import com.redhat.rhn.domain.rhnpackage.PackageSuggests;
import com.redhat.rhn.domain.rhnpackage.PackageSupplements;
import com.redhat.rhn.domain.role.RoleImpl;
import com.redhat.rhn.domain.scc.SCCOrderItem;
import com.redhat.rhn.domain.scc.SCCRegCacheItem;
import com.redhat.rhn.domain.scc.SCCRepository;
Expand All @@ -84,14 +104,37 @@
import com.redhat.rhn.domain.scc.SCCRepositoryNoAuth;
import com.redhat.rhn.domain.scc.SCCRepositoryTokenAuth;
import com.redhat.rhn.domain.scc.SCCSubscription;
import com.redhat.rhn.domain.server.Capability;
import com.redhat.rhn.domain.server.ClientCapability;
import com.redhat.rhn.domain.server.ClientCapabilityId;
import com.redhat.rhn.domain.server.CustomDataValue;
import com.redhat.rhn.domain.server.EntitlementServerGroup;
import com.redhat.rhn.domain.server.InstalledPackage;
import com.redhat.rhn.domain.server.ManagedServerGroup;
import com.redhat.rhn.domain.server.MinionServer;
import com.redhat.rhn.domain.server.MinionServerFactory;
import com.redhat.rhn.domain.server.MinionSummary;
import com.redhat.rhn.domain.server.NetworkInterface;
import com.redhat.rhn.domain.server.Pillar;
import com.redhat.rhn.domain.server.Server;
import com.redhat.rhn.domain.server.ServerAppStream;
import com.redhat.rhn.domain.server.ServerGroup;
import com.redhat.rhn.domain.server.ServerGroupType;
import com.redhat.rhn.domain.server.ServerPath;
import com.redhat.rhn.domain.server.ServerPathId;
import com.redhat.rhn.domain.server.ansible.AnsiblePath;
import com.redhat.rhn.domain.server.ansible.InventoryPath;
import com.redhat.rhn.domain.server.ansible.PlaybookPath;
import com.redhat.rhn.domain.server.virtualhostmanager.VirtualHostManagerNodeInfo;
import com.redhat.rhn.domain.task.Task;
import com.redhat.rhn.domain.token.Token;
import com.redhat.rhn.domain.token.TokenChannelAppStream;
import com.redhat.rhn.domain.user.AddressImpl;
import com.redhat.rhn.domain.user.StateChange;
import com.redhat.rhn.domain.user.legacy.PersonalInfo;
import com.redhat.rhn.domain.user.legacy.UserImpl;
import com.redhat.rhn.domain.user.legacy.UserInfo;
import com.redhat.rhn.manager.system.ServerGroupManager;

import com.suse.cloud.domain.PaygDimensionComputation;
import com.suse.cloud.domain.PaygDimensionResult;
Expand All @@ -117,6 +160,16 @@ private AnnotationRegistry() {
}

private static final List<Class<?>> ANNOTATION_CLASSES = List.of(
// AvailableChannelsView.class,
Channel.class,
ChannelArch.class,
ClonedChannel.class,
MinionSummary.class,
MinionServer.class,
Server.class,
AccessToken.class,
MinionServerFactory.class,
ServerGroupType.class,
ImageStore.class,
ImageStoreType.class,
DockerfileProfile.class,
Expand Down Expand Up @@ -203,8 +256,43 @@ private AnnotationRegistry() {
ServerAppStream.class,
AppStream.class,
AppStreamApi.class,
TokenChannelAppStream.class
);
TokenChannelAppStream.class,
CryptoKey.class,
CryptoKeyType.class,
SslCryptoKey.class,
TokenChannelAppStream.class,
UserImpl.class,
CustomDataValue.class,
NetworkInterface.class,
InstalledPackage.class,
ServerPath.class,
ServerPathId.class,
ClientCapabilityId.class,
ClientCapability.class,
Capability.class,
ConfigChannel.class,
ConfigChannelType.class,
ConfigFile.class,
ProvisionState.class,
EntitlementServerGroup.class,
ServerGroup.class,
ManagedServerGroup.class,
AddressImpl.class,
UserGroupMembers.class,
UserGroupImpl.class,
RoleImpl.class,
ServerGroupManager.class,
AppStreamApiKey.class,
Org.class,
OrgAdminManagement.class,
OrgConfig.class,
TemplateString.class,
Token.class,
UserGroupMembersId.class,
PersonalInfo.class,
UserInfo.class,
StateChange.class
);

/**
* Returns the list of all available hibernate annotation classes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.EmptyInterceptor;
import org.hibernate.type.StringType;
import org.hibernate.type.Type;

import java.io.Serializable;
Expand Down Expand Up @@ -59,8 +58,7 @@ protected static boolean emptyStringToNull(Object entity, Serializable id,
boolean modified = false;

for (int i = 0; i < types.length; i++) {
// type is string (VARCHAR) and state is empty string
if ((types[i] instanceof StringType) && "".equals(state[i])) {
if ("".equals(state[i])) {
if (LOG.isDebugEnabled()) {
LOG.debug("Object {} is setting empty string {}", entity.getClass().getCanonicalName(),
propertyNames[i]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 SUSE LLC
* Copyright (c) 2009--2012 Red Hat, Inc.
*
* This software is licensed to you under the GNU General Public License,
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
Expand All @@ -12,17 +12,10 @@
* granted to use or replicate Red Hat trademarks that are incorporated
* in this software or its documentation.
*/
package com.redhat.rhn.domain.errata;
package com.redhat.rhn.common.hibernate.converter;

import org.hibernate.type.YesNoType;

public class YesNoConverterString extends YesNoType {

/**
* AdvisoryStatusEnumType
*/
public class AdvisoryStatusEnumType extends CustomEnumType<AdvisoryStatus, String> {
/**
* Constructor
*/
public AdvisoryStatusEnumType() {
super(AdvisoryStatus.class, String.class, AdvisoryStatus::getMetadataValue,
s -> AdvisoryStatus.fromMetadata(s).orElse(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ public boolean aclHasPtfRepositories(Map<String, Object> ctx, String[] params) {
// Evaluate if any of the subscript channel refers to a PTF repository
return server.getChannels()
.stream()
.map(channel -> channel instanceof ClonedChannel ? channel.getOriginal() : channel)
.map(channel -> channel.asCloned().map(ClonedChannel::getOriginal).orElse(channel))
.flatMap(c -> c.getSources().stream())
.map(ContentSource::getSourceUrl)
.anyMatch(url -> url.contains("/PTF/"));
Expand Down
3 changes: 2 additions & 1 deletion java/code/src/com/redhat/rhn/domain/BaseDomainHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
Expand All @@ -31,7 +32,7 @@
* DB table: web_contact
*/
@MappedSuperclass
public abstract class BaseDomainHelper {
public abstract class BaseDomainHelper implements Serializable {
private Date created = new Date();
private Date modified;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,25 +683,6 @@ select {ra.*}
]]>
</query>

<query name="Action.findMinionSummaries">
<![CDATA[
SELECT new com.redhat.rhn.domain.server.MinionSummary(
sa.server.id,
s.minionId,
s.digitalServerId,
s.machineId,
c.label,
s.os
)
FROM ServerAction AS sa
JOIN sa.server AS s
JOIN s.contactMethod AS c
WHERE type(s) = com.redhat.rhn.domain.server.MinionServer
AND action_id = :id
AND sa.status IN (:allowedStatues)
]]>
</query>

<sql-query name="Action.findServerIds">
<![CDATA[
select sa.server_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void testFindByLabel() throws Exception {
private ActionType lookupByLabel(String label) {
Session session = HibernateFactory.getSession();
return (ActionType) session.getNamedQuery("ActionType.findByLabel")
.setString("label", label)
.setParameter("label", label)
//Retrieve from cache if there
.setCacheable(true)
.uniqueResult();
Expand Down
29 changes: 14 additions & 15 deletions java/code/src/com/redhat/rhn/domain/audit/ScapFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.criterion.Restrictions;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -77,8 +76,8 @@ public static void delete(XccdfTestResult tr) {
public static void clearTestResult(long serverId, long actionId) {
List<XccdfTestResult> results = getSession()
.getNamedQuery("XccdfTestResult.findByActionId")
.setLong("serverId", serverId)
.setLong("actionId", actionId)
.setParameter("serverId", serverId)
.setParameter("actionId", actionId)
.list();
results.forEach(ScapFactory::delete);
}
Expand All @@ -89,8 +88,7 @@ public static void clearTestResult(long serverId, long actionId) {
* @return the {@link XccdfBenchmark} if any
*/
public static Optional<XccdfBenchmark> lookupBenchmarkById(long benchmarkId) {
return Optional.ofNullable(
(XccdfBenchmark)getSession().get(XccdfBenchmark.class, benchmarkId));
return Optional.ofNullable(getSession().get(XccdfBenchmark.class, benchmarkId));
}

/**
Expand All @@ -99,7 +97,7 @@ public static Optional<XccdfBenchmark> lookupBenchmarkById(long benchmarkId) {
* @return the {@link XccdfIdent} if any
*/
public static Optional<XccdfIdent> lookupIdentById(long identId) {
return Optional.ofNullable((XccdfIdent)getSession().get(XccdfIdent.class, identId));
return Optional.ofNullable(getSession().get(XccdfIdent.class, identId));
}

/**
Expand All @@ -108,20 +106,21 @@ public static Optional<XccdfIdent> lookupIdentById(long identId) {
* @return the {@link XccdfProfile} if any
*/
public static Optional<XccdfProfile> lookupProfileById(long profileId) {
return Optional.ofNullable(
(XccdfProfile)getSession().get(XccdfProfile.class, profileId));
return Optional.ofNullable(getSession().get(XccdfProfile.class, profileId));
}

/**
* Find a {@link XccdfRuleResultType} by id.
* @param label label id
* @return the {@link XccdfRuleResultType} if any
* Queries an XccdfRuleResultType by its label.
*
* @param label the label of the XccdfRuleResultType
* @return optional of XccdfRuleResultType
*/
public static Optional<XccdfRuleResultType> lookupRuleResultType(String label) {
return getSession().createCriteria(XccdfRuleResultType.class)
.add(Restrictions.eq("label", label))
.list()
.stream().findFirst();
String sql = "SELECT * FROM rhnXccdfRuleResultType WHERE label = :label";
XccdfRuleResultType result =
getSession().createNativeQuery(sql, XccdfRuleResultType.class)
.setParameter("label", label).getResultStream().findFirst().orElse(null);
return Optional.ofNullable(result);
}

/**
Expand Down
Loading
Loading