小编woy*_*aru的帖子

Hibernate:行太多了

我在使用Hibernate从数据库中获取行时遇到问题.当我想只获得一行时,我收到的是20.当我想从表中获得大约1.5k行的所有行时,我收到了15.2k行.该表的实体类具有复合主键.

这是获取所有行的代码:

Criteria criteria = getSession().createCriteria(type);
criteria.setCacheable(true).setCacheRegion(BaseEntity.PACKAGE);
criteria.list();
Run Code Online (Sandbox Code Playgroud)

这是我的实体类:

@javax.persistence.Entity
@Table(name = "my_table")
public class My extends MyEntity<MyPK> {

    @EmbeddedId
    private MyPK id;

    @Column(name = "text", nullable = false)
    protected String text;

    @ManyToOne
    @JoinColumn(name = "property", nullable = false, insertable = false, updatable = false)
    protected Option option;

    @Override
    public MyPK getId() {
        return id;
    }

    @Override
    public void setId(MyPK id) {
        this.id = id;
    }

    //getters and setter
}
Run Code Online (Sandbox Code Playgroud)

这是MyPK课程:

@Embeddable
public class MyPK implements Serializable { …
Run Code Online (Sandbox Code Playgroud)

java persistence hibernate criteria

5
推荐指数
1
解决办法
2835
查看次数

将外键作为复合主键的一部分进行级联保存实体对象

我想QuestionCompletion使用所有子元素坚持我班的对象。这些孩子之一具有复合主键。作为该主键的一部分,我还具有另一个实体的外键。结果,我得到这个错误:

Exception caught during request processing: javax.ejb.EJBTransactionRolledbackException:
could not set a field value by reflection setter of com.example.model.domain.QuestionCompletionAnswerPK.questionCompletionId
javax.ejb.EJBTransactionRolledbackException: could not set a field value by reflection
setter of com.example.model.domain.QuestionCompletionAnswerPK.questionCompletionId
Run Code Online (Sandbox Code Playgroud)

最后一个“由...引起”当然是NullPointerException

Caused by: java.lang.NullPointerException
Run Code Online (Sandbox Code Playgroud)

这是我的代码的一部分。最后一行导致错误。

QuestionCompletion questionCompletion = new QuestionCompletion();
List<QuestionCompletionAnswer> answers = new ArrayList<QuestionCompletionAnswer>();
for (;;) { // loop isn't important; it's loop for answers
    ExtendedQuestion extendedQuestion = new ExtendedQuestion();
    extendedQuestion.setId(extendedQuestionId); //extendedQuestionId is known to me in that place
    for (;;) { // loop isn't …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate jpa spring-mvc

5
推荐指数
1
解决办法
7177
查看次数

当App正在运行且可见时,如何在对话框中显示Android通知

我的问题有点复杂,所以我将简要描述一下我想要实现的目标.我的应用程序从GCM任何问题接收消息.当应用程序正在运行并且对用户可见时,我没有在操作栏中显示通知,我在对话框警报中显示此消息.我BroadcastReceiver用来实现这一目标.

主要问题是,当应用程序立即收到一些通知时.当主要活动对用户可见时,第一个通知将显示在对话框警报中.接下来将像通常的Android通知一样放在Action Bar中.此时,用户会处理对话框警报,并将从操作栏中选择下一个通知.此刻我正在使用我的CloudService(延伸IntentService)intent旗帜Intent.FLAG_ACTIVITY_CLEAR_TOPPendingIntent.FLAG_UPDATE_CURRENT旗帜pendingIntent.PendingIntent只是contentIntent为我的NotificationCompat.Builder.

它以这种方式工作,对于每个用户点击Action Bar的通知,我的活动正在刷新(浮动到较低的设备边缘,然后从上边缘浮动,带有消息的对话框警报 - 我从意图获取额外的onResume方法).这个动作很好.在这种情况下,Activity只有一个实例 - 在打开几个通知时,我不必突破同一活动的几个实例.但最大的问题是,当用户在每种情况下选择任何通知时,最后一个将在对话框警报中打开.intent尽管如此,活动似乎也是一样的PendingIntent.FLAG_UPDATE_CURRENT.

CloudService处理云消息有两种方法:

private void showNotification(Bundle extras) {
    notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    String message = extras.getString(CloudMetaData.MESSAGE);

    if (App.isMyActivityVisible() && !CloudMessageDialogFragment.isAttached()) {
        sendBroadcast(message, NOTIFICATION_ID);
    } else {
        Intent intent = new Intent(this, MyParentActivity.class);
        intent.putExtra(CloudMetaData.MESSAGE, message);
        intent.putExtra(CloudMetaData.NOTIFICATION_ID, NOTIFICATION_ID);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, …
Run Code Online (Sandbox Code Playgroud)

android android-intent android-notifications android-activity

5
推荐指数
1
解决办法
3083
查看次数

缓存SpringMVC应用程序中的HTTP响应

我希望我的Spring Controller能够缓存返回的内容.我发现了很多关于如何禁用缓存的问题.我想知道如何启用缓存.我的控制器看起来像这样:

@Controller
public class SimpleController {

    @RequestMapping("/webpage.htm")
    public ModelAndView webpage(HttpServletRequest request, 
                                HttpServletResponse response) {
        ModelAndView mav = new ModelAndView("webpage");
        httpServletResponse.setHeader(“Cache-Control”, “public”);
        //some code
        return mav;
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我添加了以下行:httpServletResponse.setHeader(“Cache-Control”, “public”);设置缓存,但在刷新此页面的浏览器中,我仍然得到相同的状态结果:200 OK.我怎样才能达到效果304 not modified?我可以@ResponseStatus(value = HttpStatus.NOT_MODIFIED)在这个方法上设置注释,但它只是状态还是实际的缓存?

java spring caching spring-mvc

4
推荐指数
1
解决办法
1499
查看次数

更改首选项类别标签颜色背景

我想改变偏好类别bakcgorund的颜色.在我的带有首选项的xml中,我有以这种方式键入的类别:

<PreferenceCategory android:title="Synchronization"> ... </PreferenceCategory>
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用xml属性更改背景:

android:widgetLayout="@style/PreferenceCategory"
Run Code Online (Sandbox Code Playgroud)

我的styles.xml中的代码如下所示:

<resources>
<style name="PreferenceCategory">
    <item name="android:background">@color/my_blue</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)

但它不能正常工作.我有致命的例外:

08-13 01:43:09.111: WARN/dalvikvm(1329): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): FATAL EXCEPTION: main
08-13 01:43:09.141: ERROR/AndroidRuntime(1329): android.content.res.Resources$NotFoundException: Resource ID #0x7f090006
08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.content.res.Resources.getValue(Resources.java:892)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:1869)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.content.res.Resources.getLayout(Resources.java:731)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.preference.Preference.onCreateView(Preference.java:416)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.preference.Preference.getView(Preference.java:389)
08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:221)
08-13 …
Run Code Online (Sandbox Code Playgroud)

android android-preferences

3
推荐指数
1
解决办法
1万
查看次数

Hibernate with ProjectionList错误:无法强制转换为[Ljava.lang.Object;

我的对象的returnig列表有问题.我正在使用实体类,@EmbeddedId正如你在下面的代码中看到的那样,其中有两行文字相似:id.*.

Criteria cr = createCriteria(true);
cr.add(Restrictions.eq("id.qwerty", "QWERTY"));

ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.property("id.qwerty"));
projectionList.add(Projections.property("text"));
projectionList.add(Projections.property("id.property"));
cr.setProjection(projectionList);
cr.setResultTransformer(Transformers.aliasToBean(My.class));

List<My> myList = (List<My>) cr.list();

return myList;
Run Code Online (Sandbox Code Playgroud)

在线List<My> messagesList = (List<My>) cr.list();,发生以下错误.

java.lang.ClassCastException: my.package.dao.domain.My cannot be cast to [Ljava.lang.Object;
at org.hibernate.cache.StandardQueryCache.put(StandardQueryCache.java:104)
at org.hibernate.loader.Loader.putResultInQueryCache(Loader.java:2274)
at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2206)
at org.hibernate.loader.Loader.list(Loader.java:2164)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1706)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
Run Code Online (Sandbox Code Playgroud)

我如何定义CriteriaProjectionList在这种情况下?

编辑

@javax.persistence.Entity
@Table(name = "my_table")
public class My extends MyEntity<MyPK> {

   @EmbeddedId
   private MyPK id;

   @Column(name = "text", nullable = …
Run Code Online (Sandbox Code Playgroud)

java hibernate projection criteria-api

3
推荐指数
1
解决办法
7316
查看次数

使用UserDetailsS​​ervice和Hibernate登录SpringSecurity表单

我一直在使用Spring Security在SpringMVC中创建有用的表单登录.我是新手,也是Hibernate.我想创建简单的表单登录,它可以提供对我的Web应用程序的访问.

我使用SpringSource Tool Suite创建了我的项目并选择了Spring Template Project.它使用Maven,我也用Hibernate类生成带注释和hibernate.cfg.xml.在我的数据库(HSQLDB)中,我有三个表:users,roles和users_roles.第三个包含user_id和role_id,因此它存储有关用户角色的信息.我已经通过Hibernate成功生成了类.

我已经开始编写实现UserDetailsS​​ervice的类了.但我不知道如何正确地做到这一点.在spring-security.xml我已经定义了这样的bean:

<bean id="userDetailsService" class="hutter.pl.services.HutterUserDetailsService" />
Run Code Online (Sandbox Code Playgroud)

我想使用sha-256和saltSource进行哈希处理.

<bean class="org.springframework.security.authentication.dao.ReflectionSaltSource" id="saltSource">
    <property name="userPropertyToUse" value="username"/>
</bean>

<security:authentication-manager>   
    <security:authentication-provider user-service-ref="userDetailsService">
        <security:password-encoder hash="sha-256">
            <security:salt-source ref="saltSource" />
        </security:password-encoder> 
    </security:authentication-provider>
</security:authentication-manager>
Run Code Online (Sandbox Code Playgroud)

我应该使用这个解决方案:https://stackoverflow.com/a/1654488/845220?Hibernate已经生成了类:RolesHome,Roles,Users,UsersHome,UsersRoles,UsersRolesHome.但我真的不知道如何使用这些Hibernates类来授权用户.

@Service("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {

    @Transactional(readOnly = true)
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {   
        UsersHome usersHome = new UsersHome();
       //Users user = ...       
       //...            
       return null;     
    }   
}
Run Code Online (Sandbox Code Playgroud)

你能给我一些提示吗?

编辑: 我试图public Users findByLogin(String login)UsersHome …

spring hibernate spring-mvc spring-security

1
推荐指数
1
解决办法
8177
查看次数

如何使用string.gsub替换Lua中的括号?

我有一个函数,用于用几个字符或数字替换一些单词。我string.gsub()以这种方式使用函数:

string.gsub(line, "[0-9%a%s/,-]+", "\t")
Run Code Online (Sandbox Code Playgroud)

这对于包含数字、字母、空格,、 和 的字符串非常有效/。我也想替换像(和这样的括号)。但是简单地插入()到我的模式中是行不通的。我也尝试过%(%)但没有成功。如何在string.gsub()方法中使用模式替换 Lua 中的括号?

lua string-substitution lua-patterns

1
推荐指数
1
解决办法
2474
查看次数

在DispatcherServlet中找不到带有URI [/ myappname /]的HTTP请求的映射,名称为"appServlet"

No mapping found for HTTP request with URI [/myappname/] in DispatcherServlet with name 'appServlet'在JBoss上启动项目时遇到错误.解决此处描述的另一个问题后出现此问题:在将更改访问方法更改为会话后,"找不到当前线程的会话"

在一切正常之前.我正在使用Apache Tiles 2.我正在阅读几个类似的问题,但我找不到工作解决方案.

这是我DispatcherServlet Context没有Hibernate配置的文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:beans="http://www.springframework.org/schema/beans"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<tx:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="finances.webapp" />

<!-- Handles HTTP GET requests for resources by efficiently serving up static resources in …
Run Code Online (Sandbox Code Playgroud)

java spring tiles spring-mvc

1
推荐指数
1
解决办法
2万
查看次数

从另一个onClick方法控件访问ListView控件

如何从分配给ImageView的onClick方法访问TextView,例如TextView?TextView和ImageView组成了ListView项.

在我的item.xml中,我有:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
> 
<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:onClick="onImgClick"
    />      
<TextView android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在我的ListActivity中,我有onImgClick方法:

 public void onImgClick(View v) {
    TextView tv = (TextView) v.findViewById(R.id.text);
tv.setText("Hello world!");
} 
Run Code Online (Sandbox Code Playgroud)

但View v - 这是ImageView,所以在这个方法中我有致命异常:

java.lang.NullPointerException
Run Code Online (Sandbox Code Playgroud)

在线上:

tv.setText("Hello world!");
Run Code Online (Sandbox Code Playgroud)

我知道这不是访问TextView的正确方法.而且我知道我可以根据整个ListView使用onListItemClick(ListView l,View v,int position,long id).但我想从ImageView onClick方法实现这一点.

android onclick listactivity

0
推荐指数
1
解决办法
1992
查看次数